C# how to sync windows 10 PC with CMD which needs administrator right?

john zyd 421 Reputation points
2022-11-04T13:18:52.487+00:00

Hello:
I have an issue with my Windows 10 PC when it goes into hibrate state.
During launch time, I usually set my Windows 10 PC go into hibrate state.
After launch, when I come back to work in the office, the clock seems to be a few seconds off the correct time, I have to manually sync the clock.
However, if I setup windows times service to start automatically, I can run the following DOS command to sync clock of my PC, like this:
C:\WINDOWS\system32>w32tm /resync
Sending resync command to local computer
The command completed successfully.
If I run without administrator privilage, the above command failed.
I want to write a simple C# program, which can call this simple command to sync clock of my PC, but the program has to be run with administrator privilages.
Please advise what should do?
My OS: Windows 10 Pro (Version 21H2).
Thanks,

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,819 questions
C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,192 questions
{count} votes

Accepted answer
  1. Jiale Xue - MSFT 30,126 Reputation points Microsoft Vendor
    2022-11-07T06:59:00.317+00:00

    Hi @john zyd , Welcome to Microsoft Q&A, you could try the following code to get what you wanted.

    private void button1_Click(object sender, EventArgs e)  
    {  
    	MessageBox.Show(Execute(""));//(Your dos command)  
    }  
      
    //dosCommand Dos command statement  
    public string Execute(string dosCommand)   
    {  
    	return Execute(dosCommand, 10);  
    }  
    /// <summary>  
    /// Execute the DOS command and return the output of the DOS command  
    /// </summary>  
    /// <param name="dosCommand">dos command</param>  
    /// <param name="milliseconds">Waiting for the command execution time (unit: milliseconds),  
    /// If set to 0, wait indefinitely</param>  
    /// <returns>Returns the output of the DOS command</returns>  
    public static string Execute(string command, int seconds)   
    {  
    	string output = "";  
    	//output string  
    	if (command != null && !command.Equals(""))   
    	{  
    		Process process = new Process();  
    		//Create a process object  
    		ProcessStartInfo startInfo = new ProcessStartInfo();  
    		startInfo.FileName = "cmd.exe";  
    		//Set the command to be executed  
    		startInfo.Arguments = "/C " + command;  
    		//"/C" means exit immediately after executing the command  
    		startInfo.UseShellExecute = false;  
    		//Do not use the system shell to start  
    		startInfo.RedirectStandardInput = false;  
    		//Do not redirect input  
    		startInfo.RedirectStandardOutput = true;  
    		// redirect output  
    		startInfo.CreateNoWindow = true;  
    		//Do not create a window  
    		process.StartInfo = startInfo;  
    		try   
    		{  
    			if (process.Start())//Start the process   
    			{  
    				if (seconds == 0)   
    				{  
    					process.WaitForExit();  
    					//Here infinite waiting for the process to end  
    				} else   
    				{  
    					process.WaitForExit(seconds);  
    					//Wait for the process to end, the waiting time is the specified milliseconds  
    				}  
    				output = process.StandardOutput.ReadToEnd();  
    				//Read the output of the process  
    			}  
    		}  
    		catch   
    		{  
    		}  
    		finally   
    		{  
    			if (process != null)  
    			process.Close();  
    		}  
    	}  
    	return output;  
    }  
    

    Best Regards,
    Jiale


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


0 additional answers

Sort by: Most helpful