Visual Basic using CMD as admin and running commands

EDN Admin

Well-known member
Joined
Aug 7, 2010
Messages
12,794
Location
In the Machine
Hi All,
I have seen many posts related to this topic but am failing at actually getting any results. A friend of mine asked me if i could make him a program so that there is a UI compared to usually just running .bat files. What i was trying to do was open up the
command prompt as admin and run the commands he gave me for synchronizing the computers clock and my computer is not reflecting at all that it is working and i cant figure out why. I have it currently so the prompt doesnt close because i wanted to be able
to view input/output but it just displays a blank box. Here is the code i was trying at my last attempt, note that it only has one of the commands for synchronizing the time because i was trying to debug:
<pre class="prettyprint lang-vb Imports System
Imports System.Diagnostics
Imports System.ComponentModel


Public Class MainForm

Private Results As String
Private Delegate Sub delUpdate()
Private Finished As New delUpdate(AddressOf UpdateText)

Private Sub UpdateText()
ResultsTxt.Text = Results
End Sub

Private Sub ActivateBtn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ActivateBtn.Click
Dim CMDThread As New Threading.Thread(AddressOf CMDAutomate)
CMDThread.Start()
End Sub
Private Sub CMDAutomate()

Dim myprocess As New Process
Dim StartInfo As New System.Diagnostics.ProcessStartInfo
Dim s As String = ""
StartInfo.FileName = "runas" starts cmd window
StartInfo.RedirectStandardInput = True
StartInfo.RedirectStandardOutput = True
StartInfo.UseShellExecute = False required to redirect
StartInfo.CreateNoWindow = True creates no cmd window
myprocess.StartInfo = StartInfo
myprocess.Start()
Dim SR As System.IO.StreamReader = myprocess.StandardOutput
Dim SW As System.IO.StreamWriter = myprocess.StandardInput
SW.WriteLine("cd C:WindowsSystem32")
SW.WriteLine("net stop w32time") the commands you wish to run.....
SW.WriteLine(cmdString2)
s = SR.ReadToEnd returns results of the command window before exit
SW.WriteLine("exit") exits command prompt window
SW.Close()
SR.Close()


End Sub
End Class
[/code]
Also note i do have some unused things, this has all been a lot of trial and error. Can anyone help me out with this? The commands for resynching the time are:
net stop w32time
net start w32time
w32tm /config /manualpeerlist:time.windows.<wbr>com /syncfromflags:manual /reliable:yes /update

w32tm /resync
i have tried them in a elevated cmd manually and they work fine. Any help would be great! Thanks All.
Sam<br/>


<br/>

<br/>


View the full article
 
Back
Top