1

kindly help me to solve the following problem.

I am trying to change computer Date via C# Windows Service. The following command this command is working in C# windows application if i run it as RUN AS Administrator. but the service is not changing system date by the following command. my pc is on domain and i run this service as domain service account. also i add service@domain account to system administrator group.

string strCmdText = "/c DATE ";
System.Diagnostics.Process.Start("CMD.exe", strCmdText);

how could i run cmd as run As Administrator in C# Service which is already run as domain user.

i tried all the solutions which is provided here but still the problem is remain.

Naveed Ahmed
  • 463
  • 4
  • 11

1 Answers1

0

To run CMD in administrator privilage, you simply have to run an application in administrator privilage.

But better way to call an program from console will be using Process object (not static method), where you have more options, eg. can read output, etc.

Process p = new Process();
p.StartInfo.CreateNoWindow = true;
p.StartInfo.UseShellExecute = false;
p.StartInfo.RedirectStandardOutput = true;
p.StartInfo.FileName = "date";
p.StartInfo.Arguments = "";
p.Start();

Then to input data to the application you have to use StandardInput field of Process class

p.StandardInput.Write(string yourDate);

And at the end you can gently wait for application to finish it's job.

p.WaitForExit();

To read data from output you have to use this command:

string output = p.StandardOutput.ReadToEnd();

You can also read this line by line (if you have to):

string[] oLines = output.Split(new string[] { "\r\n", "\n" }, StringSplitOptions.None);
Skorek
  • 323
  • 2
  • 12