Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
624 views
in Technique[技术] by (71.8m points)

c# - Process.Start in WindowsSystem32 folder

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

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

1 Reply

0 votes
by (71.8m points)

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")


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...