Connecting Tech Pros Worldwide Forums | Help | Site Map

Run command line from vb.net

Newbie
 
Join Date: Oct 2008
Posts: 5
#1: Oct 20 '08
How do I run commands like cacls or any other windows commands using vb.net


I am unable to run commands using the shell method in Vb.net but I would like to be able to run other executables with command line switches from a vb.net application.

Please help.

joedeene's Avatar
Site Addict
 
Join Date: Jul 2008
Location: US of A
Posts: 587
#2: Oct 20 '08

re: Run command line from vb.net


If you're trying to start other processes with arguments then the ProcessStartInfo Class will probably be useful, with the .Arguments property also. Is that what you were looking for?

joedeene
Newbie
 
Join Date: Oct 2008
Posts: 5
#3: Oct 23 '08

re: Run command line from vb.net


This is what I am trying to do,I am trying to integrate the cacls utility(Microsoft utility to remove ntfs permissions) onto a vb.net project.

I would like to run the following command using a form based application.

My form will have a textbox which will take an input of the files for which permissions need to be removed.

On a button click it should be able to run the cacls utility with the following parameters.

cacls.exe "filename" /d everyone.
the file name part will be replaced with the +textbox1.text+

any suggestions let me know.
insertAlias's Avatar
Forum Leader
 
Join Date: Apr 2008
Location: San Antonio, TX (USA)
Posts: 2,608
#4: Oct 23 '08

re: Run command line from vb.net


You'll need to use the ProcessStartInfo object.
Also, use String.Format instead of string concatenation:
Expand|Select|Wrap|Line Numbers
  1. Dim psi As ProcessStartInfo
  2. Dim procname As String = "calcs.exe"
  3. Dim filename As String = "whatever.txt"
  4. Dim args As String = String.Format("{0} /d everyone", filename)
  5. psi = New ProcessStartInfo(procname, args)
  6. Dim proc As New Process()
  7. proc.StartInfo = psi
  8. proc.Start()
  9.  
Newbie
 
Join Date: Oct 2008
Posts: 5
#5: Oct 25 '08

re: Run command line from vb.net


Quote:

Originally Posted by insertAlias

You'll need to use the ProcessStartInfo object.
Also, use String.Format instead of string concatenation:

Expand|Select|Wrap|Line Numbers
  1. Dim psi As ProcessStartInfo
  2. Dim procname As String = "calcs.exe"
  3. Dim filename As String = "whatever.txt"
  4. Dim args As String = String.Format("{0} /d everyone", filename)
  5. psi = New ProcessStartInfo(procname, args)
  6. Dim proc As New Process()
  7. proc.StartInfo = psi
  8. proc.Start()
  9.  


I was able to run the utility,just one more information,after execution I would like to make the program pass another argument -Y

Basically it is cacls.exe "filename" /d everyone

on pressing enter- it shows

Are you sure?Y/N

How would I pass the Y onto the command.
Appreciate your help in this..
Member
 
Join Date: Nov 2007
Location: Alberta, Canada
Posts: 59
#6: Oct 27 '08

re: Run command line from vb.net


Quote:

Originally Posted by venky5186

How do I run commands like cacls or any other windows commands using vb.net


I am unable to run commands using the shell method in Vb.net but I would like to be able to run other executables with command line switches from a vb.net application.

Please help.

I think i understand you...did u try using
Expand|Select|Wrap|Line Numbers
  1. Shell("calcs myfile.txt ")
replace myfile.txt with the filename you want to run it on
Plater's Avatar
Moderator
 
Join Date: Apr 2007
Location: New England
Posts: 7,161
#7: Oct 27 '08

re: Run command line from vb.net


Quote:

Originally Posted by venky5186

I was able to run the utility,just one more information,after execution I would like to make the program pass another argument -Y

Basically it is cacls.exe "filename" /d everyone

on pressing enter- it shows

Are you sure?Y/N

How would I pass the Y onto the command.
Appreciate your help in this..

Ahh, the calcs program also waits on user input?
In the ProcessStartInfo object there is a ".RedirectStandardInput" property. Set it to true.
Then you can do this after the .Start() call
myprocess.StandardInput.Write("Y");

Note: You may have to wait a few cycles before calling that.
So like:
Expand|Select|Wrap|Line Numbers
  1. myprocess.Start();
  2. Thread.Sleep(100);
  3. myprocess.StandardInput.Write("Y");//do you need to press the enter key? I think you would change it to myprocess.StandardInput.WriteLine("Y")
  4.  
Reply