Execute dos command using vb.net


All the dos commands can be executed using batch file.In the code below, I have created a batch file and some dos commands are writteb in it.Later this batch file is executed using process.


All the dos commands can be executed using batch file.In the code below, I have created a batch file and some dos commands are writteb in it.Later this batch file is executed using process.


 
Imports System.IO

Dim objProcess As New Process
Dim objWriter As StreamWriter
Dim strFile As String

Try
'The file name is Batch.bat and is saved on c:
strFile = "c:\Batch.bat"
'Create a new file if it does not exist
'else use the existing batch file.
If File.Exists(strFile) = False Then
objWriter = File.CreateText(strFile)
End If
'Here I have connected to windows 2003 server using dos command.
'The security policies of windows 2003 server
'does not allow accessing the shared folder directly.
'It prompts for username and password.
'Here the server ip is 192.168.1.24,shared folder name
'is shared,user name is admin and password is admin.
'Once the dos command
'net use \\192.168.1.25 '\SHARED /USER:admin "admin
'executes you can access the shared folder.You can
'execute other dos command as per your requirement.
objWriter.WriteLine("net use \\192.168.1.25\SHARED /USER:admin "admin"")
objWriter.Close()
'The batch file is executed using process objProcess.
objProcess.StartInfo.FileName = strFile
objProcess.Start()
Catch ex As Exception
SBO_Application.MessageBox("Error()=" & ex.Message)
Finally
objProcess = Nothing
objWriter = Nothing
End Try


Comments

Author: Jayendra Kumar18 Feb 2010 Member Level: Gold   Points : 0

This is really good information.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: