shell command from code without .bat file?

stzed

New member
Joined
Dec 7, 2006
Messages
4
Programming Experience
1-3
Can you run a command line program from shell without writing a .bat file disk and calling the .bat file with the Shell( ) method?

I would like to create my command string and run it without having to write any temporary files to disk if at all possible. Also, I would like to capture the command's return string (if any) without using a temporary file if possible.

I am sending a file to a printer using the Lpr command. In code I generate a string such as "Lpr -S $PrinterIP -P $Queue "$PathToFile" >> "$OutputFile"", I write this string to a .bat file, run the .bat file with the Shell( ) method, check the contents of $OutputFile, and delete $OutputFile and the .bat file.

There has to be a better, cleaner, way.
 
VB.NET:
process.start(stringPathapp,stringArguments)

Thread was moved to VB General forum.
 
I tried the following code and have 2 questions: 1) I have a command line window popup despite using a hidden window stype, how to I get this process to occur invisibly? 2) I get the error message below, am I forgetting something setting up my process? Thanks.

VB.NET:
PrivateFunction LPRProcess(ByVal pstrFilePath AsString, _
ByVal pstrQueue AsString, _
ByVal pstrPrinter AsString)
Dim prcLprInfo AsNew ProcessStartInfo
prcLprInfo.FileName = "Lpr"
prcLprInfo.WindowStyle = ProcessWindowStyle.Hidden
prcLprInfo.UseShellExecute = False
prcLprInfo.Arguments = "-S " & pstrPrinter & " -P " & pstrQueue & " "" & pstrFilePath & """
Dim prcLpr AsNew Process
prcLpr.Start(prcLprInfo)
prcLpr.WaitForExit()
EndFunction
Error Message:
---> System.InvalidOperationException: No process is associated with this object.
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.EnsureState(State state)
at System.Diagnostics.Process.GetProcessHandle(Int32 access, Boolean throwIfExited)
at System.Diagnostics.Process.WaitForExit(Int32 milliseconds)
at System.Diagnostics.Process.WaitForExit()
at WV.WV_oaBtc.WV_oaBtcLpr.LPRProcess(String pstrFilePath, String pstrQueue, String pstrPrinter)
at WV.WV_oaBtc.WV_oaBtcLpr.Execute(String pstrFilePath, String pstrPrinter, String pstrQueue)
 
Chances are the process was finished before you called .WaitForExit. You can check first with:
VB.NET:
if not prclpr.hasexited then prclpr.waitforexit()
try also
VB.NET:
prcLprInfo.createnowindow=true
 
I no longer get epiliptic (sp?) seizure inducing command windows flashes (yay!), but my 'no process associated with this object' error remains...

I changed prcLprInfo.Filename to "notepad" and successfully opened notepad.exe while running the code, but still get the error and the prcLpr.close( ) in my finally block does not seem to close notepad like I expected it to.


VB.NET:
Private Function LPRProcess(ByVal pstrFilePath As String, _
ByVal pstrQueue As String, _
ByVal pstrPrinter As String)
Dim prcLprInfo As New ProcessStartInfo
prcLprInfo.FileName = "Lpr"
prcLprInfo.CreateNoWindow = True
prcLprInfo.WindowStyle = ProcessWindowStyle.Hidden
prcLprInfo.UseShellExecute = False
prcLprInfo.Arguments = "-S " & pstrPrinter & " -P " & pstrQueue & " "" & pstrFilePath & """
Dim prcLpr As New Process
Try
prcLpr.Start(prcLprInfo)
If Not prcLpr Is Nothing AndAlso Not prcLpr.HasExited Then prcLpr.WaitForExit()
Catch ex As Exception
Throw New Exception("Error running LPR process: " & ex.Message)
Finally
prcLpr.Close()
End Try
End Function

VB.NET:
---> System.Exception: IN WV_oaBtc.WV_oaBtcLpr AT STAGE: Run LPR using Process.Start( )
---> System.Exception: Error running LPR process: No process is associated with this object.
at WV.WV_oaBtc.WV_oaBtcLpr.LPRProcess(String pstrFilePath, String pstrQueue, String pstrPrinter)
at WV.WV_oaBtc.WV_oaBtcLpr.Execute(String pstrFilePath, String pstrPrinter, String pstrQueue)
 
If you read the documentation you wouldn't expect Process.Close method to close the associated process, but rather close and free the allocated resources used by the Process class instance. Process.Kill method stops the associated process.

Actually I should have noticed this other thing before, Process.Start(params) is a shared method that doesn't use a process instance, only the params is executed.

.Net 2.0 running a shared method through an instance gives a warning error, so perhaps you are not using .Net 2.0? (look at your post where is says what your primary platform is! you can change it in User Control Panel) In the same go you should also have gotten another warning, quoted:
Warning 1 Access of shared member, constant member, enum member or nested type through an instance; qualifying expression will not be evaluated.

Warning 2 Function 'LPRProcess' doesn't return a value on all code paths. A null reference exception could occur at run time when the result is used.
If you don't intend to return anything from that Function method change it to a Sub method.

Anyway, you can associate the startinfo to the process instance by setting the Process.StartInfo property to your configured instance of StartInfo:
VB.NET:
prcLpr.startinfo=prcLprInfo
prcLpr.start()
 
Thanks for all the help. I once I followed your advice I got the thread working, then found out that I needed to add another set of double quotes to my parameters string because it will literally looking for 'pstrFilePath' instead of the value of of string pstrFilePath...

My working code:

VB.NET:
[SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Private[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function[/COLOR][/SIZE][SIZE=2] LPRProcess([/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] pstrFilePath [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], _
[/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] pstrQueue [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2], _
[/SIZE][SIZE=2][COLOR=#0000ff]ByVal[/COLOR][/SIZE][SIZE=2] pstrPrinter [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String[/COLOR][/SIZE][SIZE=2])
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] prcLprInfo [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] ProcessStartInfo
prcLprInfo.FileName = "Lpr"
prcLprInfo.CreateNoWindow = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]prcLprInfo.WindowStyle = ProcessWindowStyle.Hidden
prcLprInfo.UseShellExecute = [/SIZE][SIZE=2][COLOR=#0000ff]False
[/COLOR][/SIZE][SIZE=2]prcLprInfo.RedirectStandardOutput = [/SIZE][SIZE=2][COLOR=#0000ff]True
[/COLOR][/SIZE][SIZE=2]prcLprInfo.Arguments = "-S " & pstrPrinter & " -P " & pstrQueue & " """ & pstrFilePath & """"
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] prcLpr [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Process
[/SIZE][SIZE=2][COLOR=#0000ff]Dim[/COLOR][/SIZE][SIZE=2] strOutput [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]String
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2]Stage = "Run Process.Start( )"
prcLpr.StartInfo = prcLprInfo
prcLpr.Start()
strOutput = prcLpr.StandardOutput.ReadToEnd()
Stage = "Process started, wait for it to exit"
[/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Not[/COLOR][/SIZE][SIZE=2] prcLpr.HasExited [/SIZE][SIZE=2][COLOR=#0000ff]Then[/COLOR][/SIZE][SIZE=2] prcLpr.WaitForExit()
[/SIZE][SIZE=2][COLOR=#0000ff]Catch[/COLOR][/SIZE][SIZE=2] ex [/SIZE][SIZE=2][COLOR=#0000ff]As[/COLOR][/SIZE][SIZE=2] Exception
[/SIZE][SIZE=2][COLOR=#0000ff]Throw[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Exception("Error running LPR process: " & ex.Message)
[/SIZE][SIZE=2][COLOR=#0000ff]Finally
[/COLOR][/SIZE][SIZE=2]prcLpr.Close()
prcLpr.Dispose()
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Try
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]If[/COLOR][/SIZE][SIZE=2] strOutput.Length > 0 [/SIZE][SIZE=2][COLOR=#0000ff]Then
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]Throw[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]New[/COLOR][/SIZE][SIZE=2] Exception("LPR ERROR: " & strOutput)
[/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]If
[/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff]End[/COLOR][/SIZE][SIZE=2] [/SIZE][SIZE=2][COLOR=#0000ff]Function
[/COLOR][/SIZE]
 
Back
Top