通过C#代码以其他用户身份静默运行.bat文件 [英] Run .bat file through C# code as different user silently

查看:615
本文介绍了通过C#代码以其他用户身份静默运行.bat文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我每隔几秒钟运行一个批处理文件,以使用以下代码与服务器进行时间同步:

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?

我所看到的一种行为是,如果我使用UseShellExecute = true运行相同的代码并且不提供用户名和密码,那么命令窗口将不会闪烁.

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.

所以基本上我想以c#代码作为其他用户来静默地运行.bat文件.

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

推荐答案

由于您要传递用户名和密码,因此不遵守CreateNoWindow参数.这是Windows中的功能(即错误).这是已有5年历史的连接详细信息:

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()调用advapi32.dll的 事件中的CreateProcessWithLogonW 用户提供用户名并 密码和CreateProcessWithLogonW 总是打开一个新窗口. 不幸的是,没有解决方法 为此行为

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

以下给出了不创建窗口"选项的出色概述: http://blogs.msdn.com/b/jmstall/archive/2006/09/28/createnowindow.aspx 这也指出了有关该主题的msdn文档中的错误.

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.

这个stackoverflow答案有一个很好的概述: 如何在运行批处理文件?

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.

希望这会有所帮助 磅

这篇关于通过C#代码以其他用户身份静默运行.bat文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆