Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C# Cannot start a system EXE process

I have this code:

 Process myProcess = new Process();

 myProcess.StartInfo.UseShellExecute = true;
 myProcess.StartInfo.FileName = "rdpclip.exe";
 myProcess.Start();

to start an exe file which is located in system32

I always get an error that, the system file cannot be found. In windows 2008 server.

Even if I set the StartupInfo.FileName="c:\\windows\\system32\\rdpclip.exe" it still does not find the file !?

It works if I place the file in other folder, but in System32 it does not start. I just need to kill this process and start again, but I never thought that in C# is such a pain to do such a simple thing ?!

like image 356
Mario Avatar asked Feb 18 '23 09:02

Mario


2 Answers

This error is misleading because it usually means you to do not have permission to that folder. Try building your program, then right click the resulting .exe and click 'run as administrator'.

like image 139
Despertar Avatar answered Feb 28 '23 07:02

Despertar


Try this (you'll need to import System.Runtime.InteropServices):

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64DisableWow64FsRedirection(ref IntPtr ptr);

[DllImport("kernel32.dll", SetLastError = true)]
public static extern bool Wow64RevertWow64FsRedirection(IntPtr ptr);

IntPtr ptr = IntPtr.Zero;
if(Wow64DisableWow64FsRedirection(ref ptr))
{
    Process myProcess = new Process();
    myProcess.StartInfo.UseShellExecute = true;
    myProcess.StartInfo.FileName = "rdpclip.exe";
    myProcess.Start();
    Process.Start(myProcess);
    Wow64RevertWow64FsRedirection(ptr);    
}
like image 36
Kramii Avatar answered Feb 28 '23 07:02

Kramii