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
772 views
in Technique[技术] by (71.8m points)

c# - Running cmd commands with Administrator rights

How can I run the command **cd..** behind the scenes of a Windows Form? (i.e. the user cannot see it)

Thanks.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

You may initialize a new System.Diagnostics.ProcessStartInfo which has the information required for your process to start in addition to WindowStyle that indicates the window state to use when the process is started which can be Hidden, Maximized, Minimized or Normal. In your case, we will set this as Hidden so that the process which will be started won't be able to receive either input nor show the output from/to the user.

Example

System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"System32cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%System32cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo

Screenshot

The following screenshot represents the Task Manager showing one process which was started by our application. However, its Window is not visible.

The process is running without showing its Window

Notice: The process started will not terminate even if you close your application.

Additionally, to run a Process as an Administrator you may set the Verb property of the process start info to runas

Example

System.Diagnostics.ProcessStartInfo myProcessInfo = new System.Diagnostics.ProcessStartInfo(); //Initializes a new ProcessStartInfo of name myProcessInfo
myProcessInfo.FileName = Environment.ExpandEnvironmentVariables("%SystemRoot%") + @"System32cmd.exe"; //Sets the FileName property of myProcessInfo to %SystemRoot%System32cmd.exe where %SystemRoot% is a system variable which is expanded using Environment.ExpandEnvironmentVariables
myProcessInfo.Arguments = "cd.."; //Sets the arguments to cd..
myProcessInfo.WindowStyle = System.Diagnostics.ProcessWindowStyle.Hidden; //Sets the WindowStyle of myProcessInfo which indicates the window state to use when the process is started to Hidden
myProcessInfo.Verb = "runas"; //The process should start with elevated permissions
System.Diagnostics.Process.Start(myProcessInfo); //Starts the process based on myProcessInfo

Notice: If you have the User Account Control enabled, you may be asked to allow the process to start with elevated permissions first if the application that tried to call this process was not running with elevated permissions.

If you would like to skip the prompt, I think that you should allow your main application to start with elevated permissions. To do this, you'll need to open your application's manifest and make sure that the following line is added

<requestedExecutionLevel level="requireAdministrator" uiAccess="false"/>

This will simply tell your application to start only with elevated permissions. So, when you call the process as an Administrator, there'll be no prompt as the process caller is being executed under an Administrator.

Thanks,
I hope you find this helpful :)


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

...