3

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.

Jonathan Kortleven
  • 613
  • 1
  • 5
  • 16
user393014
  • 445
  • 1
  • 8
  • 15

4 Answers4

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

Community
  • 1
  • 1
Leon Bambrick
  • 26,009
  • 9
  • 51
  • 75
1

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

Phil
  • 51
  • 5
0

Did you try to just specify:

process.StartInfo.CreateNoWindow=true;

?

Felice Pollano
  • 32,832
  • 9
  • 75
  • 115
0

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

Uwe Keim
  • 39,551
  • 56
  • 175
  • 291
  • The user I am running does not have privilege to impersonate other user. – user393014 Jan 10 '11 at 19:48
  • Then why are you asking for it? _"...I want to run .bat file using c# code as different user..."_ was your initial question. What is the difference between that and impersonation? – Uwe Keim Jan 10 '11 at 19:51
  • There is security restriction. See, the .bat file is the workaround for this security and it is working fine. It is just only flashing which is annoying. – user393014 Jan 10 '11 at 19:53
  • Moreover, I know the user under which I need to run the other processes with higher privileges. – user393014 Jan 10 '11 at 19:55