Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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 Platform is: x86. Current OS: Windows 8.1 x64. I'd rather not have 2 different .exes for a 32 and 64 bit os.

p.StartInfo.Verb = "runas";
p.StartInfo.FileName =
    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System),"Defrag.exe");
    //above points to c:windowssystem32defrag.exe
p.StartInfo.Arguments = @"c: /A";
p.Start();
p.WaitForExit();

I have also tried the following with no luck

p.StartInfo.FileName = 
    Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Windows), "sysnative", "Defrag.exe");

Update

Switched the app from x86 to Any CPU corrected the issue

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
604 views
Welcome To Ask or Share your Answers For Others

1 Answer

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:WindowsSysWOW64 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:WindowsSystem32defrag.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")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...