MSFN Forum: [VB.NET] how to execute a program from - MSFN Forum

Jump to content



Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

[VB.NET] how to execute a program from Rate Topic: -----

#1 User is offline   vcant 

  • Advanced Member
  • PipPipPip
  • Group: Members
  • Posts: 461
  • Joined: 15-May 03

Posted 15 September 2003 - 03:44 AM

anyone know how do i execute an external program from VS.NET code (VB.NET, C#).


#2 User is offline   b0r3d 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 821
  • Joined: 03-August 03

Posted 15 September 2003 - 05:38 AM

In VB.NET it's really rather simple.
Shell("C:\cat.txt")

or to run a system command:
shell("regedit")

Good luck with your programming! :)

#3 User is offline   fwm 

  • Newbie
  • Group: Members
  • Posts: 36
  • Joined: 28-July 03

Posted 24 October 2003 - 06:29 PM

C#

Process.Start("app.exe");

Remember to use the System.Diagnostics namespace for it to work, or else just
System.Diagnostics.Process.Start("app.exe");

That goes for VB.NET also, which is prefered instead of the Shell action, because the other way gives you much more control, and you can set specific actions for your execution!

#4 User is offline   antcheah 

  • Group: Members
  • Posts: 2
  • Joined: 03-November 03

Posted 03 November 2003 - 01:08 PM

Man i just wanna thanx you guys...

have been looking for a way to execute defragmenter with vb.net

been looking for weeks... now i know

Shell("defrag C:")

is that simple...to execure a program

ps: do you guys know how to execure a program from a specific directory?


antcheah :)

#5 User is offline   b0r3d 

  • Friend of MSFN
  • PipPipPipPipPip
  • Group: Members
  • Posts: 821
  • Joined: 03-August 03

Posted 03 November 2003 - 09:00 PM

the same way will work. Just put the path in there.
shell("C:\program\program.exe")

#6 User is offline   antcheah 

  • Group: Members
  • Posts: 2
  • Joined: 03-November 03

  Posted 09 November 2003 - 07:57 AM

How about assigning a variable ???

like

DIM X as string = "defrag C:"
X = """ & X & """

shell(X)


it does not seem to work if i set a variable to assign a value to the shell

i would have to type hard code it for it to work...any alternative?
ps: if assign X to a label would get
"defrag C:"

antcheah

#7 User is offline   qallaf 

  • Newbie
  • Group: Members
  • Posts: 22
  • Joined: 17-June 03

Posted 20 November 2003 - 06:15 AM

Shell("http://www.link.com")

can that work??

#8 User is offline   IceBoxQS 

  • Group: Members
  • Posts: 1
  • Joined: 23-November 03

  Posted 23 November 2003 - 09:06 PM

I hope someone can help.

I am trying to do about the same thing but I can't get the exe file to work if I put any switches after it.

For example:

shell("netstat.exe -ano > c:\temp.txt")

This just flashes the command window and I get no output file on my c drive.

I have also tried:

shell("netstat.exe -ano > ""c:\temp.txt""")

I tried this because in the help section they say the path can be lost if you don't double quote the path. Anyhow it didn't help any.

I've tried a number of other methods like tossing the string in a variable like the guy above and then putting the variable in the shell function. But that doesn't work either.

The only way I can get it to work is if I use:

shell("netstat.exe")

I have also tried these different ways using Process.Start
And putting the switches in for the arguements. Anyhow that is where I'm at and I'm just spinning my wheels currently. :)

Does anyone know how to get the switches to work after an exe in vb .net? :rolleyes:


Update:

Well I still haven't figured out how to do it in VB.net. But I can call on a batch file from vb.net, which then fires: netstat -ano > c:\netstat.txt

It worked that way but that is round about and ugly. Anyone have something more sound? Please!

#9 User is offline   will_orangerocket 

  • Group: Members
  • Posts: 1
  • Joined: 26-November 03

Posted 26 November 2003 - 03:54 PM

Folks, don't use Shell in .NET. Use what has already been suggested, Process.Start. I couldn't even get Shell to work in my build of Visual Studio for whatever reason but Process.Start worked great. Obviously, Microsoft wants us to get away from the Shell command. I'm more than happy to oblige.

#10 User is offline   siwik75 

  • Group: Members
  • Posts: 1
  • Joined: 10-December 03

Posted 10 December 2003 - 05:24 AM

Usefull infos! But I have a question:

let's assume you want to run telnet toward a server
and send over commands and retrieve the output produced.

Is there a way to accomplish this task?

thank you all and have worthy developing!

Simone
ITALY

#11 User is offline   fwm 

  • Newbie
  • Group: Members
  • Posts: 36
  • Joined: 28-July 03

Posted 17 December 2003 - 05:05 AM

antcheah, on Nov 9 2003, 02:57 PM, said:

How about assigning a variable ???

like

DIM X as string = "defrag C:"
X = """ & X & """

shell(X)


it does not seem to work if i set a variable to assign a value to the shell

i would have to type hard code it for it to work...any alternative?
ps: if assign X to a label would get
"defrag C:"

antcheah

As I said, you should use the System.Diagnostics namespace, and the class Process will take care of that for you.

Like an example:

using System.Diagnostics;

private void startProcess(string Path, string fileName)
{
Process install = new Process();
install.StartInfo.WorkingDirectory = path;
install.StartInfo.FileName = fileName;
install.Start();
}

And then in your code you execute like this:

startProcess("C:\", "file.exe");

Above was just to show the options, you could do it like this:

Process.Start("C:\file.exe");

You see the potential?

#12 User is offline   fwm 

  • Newbie
  • Group: Members
  • Posts: 36
  • Joined: 28-July 03

Posted 17 December 2003 - 05:10 AM

will_orangerocket, on Nov 26 2003, 10:54 PM, said:

Folks, don't use Shell in .NET.  Use what has already been suggested, Process.Start.  I couldn't even get Shell to work in my build of Visual Studio for whatever reason but Process.Start worked great.  Obviously, Microsoft wants us to get away from the Shell command.  I'm more than happy to oblige.

That's probably because you tried in C#, the Shell command is VB(.NET) only!

#13 User is offline   /\/\o\/\/ 

  • Member
  • PipPip
  • Group: Members
  • Posts: 101
  • Joined: 26-April 04

Posted 15 December 2004 - 10:11 AM

ls,

below an example with redirection and parameters(arguments)
the output streams are redirected to a string that you can use in your program.

gr /\/\o\/\/

PS. for a batch file the trick is to use the FOR command to get the uotput from a vbscript to a variable

for /f %%i in ('cscript //nologo bin\GetFysicalNode.vbs %source%') do set Server=%%i


---------------------------------------------

Dim objProcess As New Process()

' Start the Command and redirect the output

objProcess.StartInfo.UseShellExecute = False
objProcess.StartInfo.RedirectStandardOutput = True
objProcess.StartInfo.CreateNoWindow = True
objProcess.StartInfo.RedirectStandardError = True
objProcess.StartInfo.FileName() = "Cluster"
objProcess.StartInfo.Arguments() = "/Cluster:" & strMachineName & " res /priv"
objProcess.Start()

strOutput = objProcess.StandardOutput.ReadToEnd()
strError = objProcess.StandardError.ReadToEnd()
objProcess.WaitForExit()

Share this topic:


Page 1 of 1
  • You cannot start a new topic
  • You cannot reply to this topic

2 User(s) are reading this topic
0 members, 2 guests, 0 anonymous users



All trademarks mentioned on this page are the property of their respective owners
Copyright © 2001 - 2011 msfn.org
Privacy Policy