Process.Start in Windows\System32 folder

后端 未结 2 1220
别跟我提以往
别跟我提以往 2020-12-20 08:09

Trying to launch a file located in System32 as administrator but it keeps telling me it doesn\'t exist.

Error: System can\'t find specified file Build Target Platfor

相关标签:
2条回答
  • 2020-12-20 08:35

    Use Environment.SystemDirectory:

    string filePath = Path.Combine(Environment.SystemDirectory, "defrag.exe");
    
    0 讨论(0)
  • 2020-12-20 08:55

    My guess would be that you are running this code on a 64-Bit Machine. If I remember correctly, the Environment.SpecialFolder.System variable returns C:\Windows\SysWOW64 on a 64-Bit machine. A quick search of the SysWOW64 Folder, and the error message is correct as "Defrag.exe" doesn't exist in the folder.

    For test purposes, I would suggest something a bit simpler i.e Process.Start(@"C:\Windows\System32\defrag.exe")

    Then you can use other variables to build your path based on the System Architecture:
    String processPath = Environment.Is64BitOperatingSystem ? Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.SystemX86), "Defrag.exe") : Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "Defrag.exe")

    0 讨论(0)
提交回复
热议问题