Run .bat file through C# code as different user silently

喜夏-厌秋 提交于 2019-12-10 22:00:04

问题


I am running one batch file every few seconds to do timesync with server using following code:

Process process = new Process();

process.StartInfo.WorkingDirectory = Environment.GetFolderPath(Environment.SpecialFolder.System);

process.StartInfo.FileName = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.System), "cmd.exe");
process.StartInfo.Arguments = @"/C C:\TimeSync.bat";
process.StartInfo.CreateNoWindow = true;
process.StartInfo.UseShellExecute = false;
process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.UserName = "username";

SecureString pwd = new SecureString();

Char[] pwdCharacters = "password".ToCharArray();
foreach (char t in pwdCharacters)
{
    pwd.AppendChar(t);
}

process.StartInfo.Password = pwd;

process.Start();
string output = process.StandardOutput.ReadToEnd();

The problem is it flashes the command windows on the screen which I don't want. How can I prevent that?

One behavior I have seen is if I run the same code with UseShellExecute = true and don't provide username and password then the command window doesn't flash.

So basically I want to run .bat file using c# code as different user silently.


回答1:


Because you are passing a user name and password, the CreateNoWindow parameters are not respected. This is a feature (i.e. bug) in windows. Here's the five year old connect details:

http://connect.microsoft.com/VisualStudio/feedback/details/98476/cmd-windows-shows-using-process-with-createnowindow-when-using-username-password-option

Process.Start() calls advapi32.dll's CreateProcessWithLogonW in the event that a user supplies a username and password, and CreateProcessWithLogonW always opens a new window. Unfortunately, there is no workaround for this behavior

An excellent overview of the create no window option is given here: http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx which also points out errors in the msdn documentation on this topic.

And there's a very good overview in this stackoverflow answer: How to hide cmd window while running a batch file?

In the end, I think you want to create a separate little app that you call out to only once, and it runs the whole time, as the escalated user. It can then perform the time sync as often as you want, in the same manner as you've described above, but without having to specify username and password. Thus there will only be one 'flash' of a console window for the entire duration of the application.

Hope this helps lb




回答2:


Change your line:

process.StartInfo.WindowStyle = ProcessWindowStyle.Minimized;

to

 process.StartInfo.WindowStyle =
 ProcessWindowStyle.Hidden;

That will hide the window and never show it.

Hope that helps!

Phil




回答3:


Did you try to just specify:

process.StartInfo.CreateNoWindow=true;

?




回答4:


You could use impersonation. I've written an impersonation class that implements the IDisposable interface and is rather straightforward to use, I hope.



来源:https://stackoverflow.com/questions/4650738/run-bat-file-through-c-sharp-code-as-different-user-silently

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!