C# New process created cannot access certain files

2019-07-13 03:11发布

问题:

I created a process in C# (say cmd.exe). But, the created exe is not able to access all of the files as a process run by the user.

The screenshot which I have embedded clearly illustrates this issue. The process (cmd) which my application created is in the left side and the one in right side is opened with a run command directly. I have made a dir cmd to execute on both of these command prompts. The difference in the count is greatly astonishing me.

Sorry it shouts that I don't have enough reputation. So just the link of the screenshot.

The Screenshot showing the difference in counts

I considered about the elevation of the application, because I thought that the process was not allowed to access all the files of the system to a standard user. So I elevated the process.

In my program, I tried to create a cmd prompt and execute the java command from it to run a jar file.

Also don't ask about the PATH variables (blah, blah, blah). Because the java.exe rests in the system32/ folder which is already in the PATH variable.

The following is the source code of a function that creates the new process.

Process process = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo(@"C:\Windows\System32\cmd.exe");
startInfo.UseShellExecute = true;
startInfo.Verb = "runas";
startInfo.Arguments = "/C java.exe";
process.StartInfo = startInfo;
process.Start();

Shortly,

  • x is a new process created with Process.start
  • x can't find a file which an usual app finds.
  • x got admin rights also
  • x is quite mysterious??

EDIT:

  • Say x is a cmd.exe, It opens C:/windows/SysWOW64 instead of the expected C:/Windows/System32 on running the command cd C:/Windows/System32/
  • How do I visit the actual System32 folder?

回答1:

Look at the file sizes, they are also different, and actually the folders are not the same. On the right is C:\windows\system32 while on the left C:\Windows\SysWOW64

Seems to be a matter of bitness. Check by the windows task manager the bitness of your processes. The one on the left should have *32 next to it. It runs as an 32 bit process and by default is in SysWow64 folder

Note: OP then changed the target platform to x64 which solved the issue



回答2:

The applications created with Visual Studio 2013 have preference to run in 32bit mode by default.

Windows x64 have strange things

  • C:\Windows\System32 - Windows System folder (system directory) for 64-bits
  • C:\Windows\SysWOW64 - Windows System folder (system directory) for 32-bit files
  • C:\Program Files - Folder for 64-bit program files
  • C:\Program Files (x86) - Folder for 32-bit program files

The Windows handles the redirection to System32 Folders based on whether they are 32-bit application or 64-bit application.

This results in the different System32 directories listed for the same command. As the command prompt created with the Process.start and run command to have been created as 32-bit process and 64-bit process respectively.

Read more on http://www.samlogic.net/articles/32-64-bit-windows-folder-x86-syswow64.htm

Changing the platform to x64 solved the issue.



标签: java c# .net cmd