473,503 Members | 1,831 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Can't run batch file?

I'd like to execute a batch file. However, I just get a quick DOS
window flash when I run this code:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo
(@"C:\mybat.bat");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.Start();
proc.WaitForExit();

If I double click the bat file, I see the DOS window with output and
everything executes. If I open a DOS window and type in the bat file,
everything works fine again. The file modifies other files. Looking
at the files that are supposed to be modified, I can see it isn't
executing via the C# code.

Any ideas?

Thanks,
Brett

Sep 12 '06 #1
12 4060

why not just:

System.Diagnostics.Process.Start((@"C:\mybat.bat") ;

Brett Romero wrote:
I'd like to execute a batch file. However, I just get a quick DOS
window flash when I run this code:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo
(@"C:\mybat.bat");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.Start();
proc.WaitForExit();

If I double click the bat file, I see the DOS window with output and
everything executes. If I open a DOS window and type in the bat file,
everything works fine again. The file modifies other files. Looking
at the files that are supposed to be modified, I can see it isn't
executing via the C# code.

Any ideas?

Thanks,
Brett
Sep 12 '06 #2

Mi************@gmail.com wrote:
why not just:

System.Diagnostics.Process.Start((@"C:\mybat.bat") ;
Same result - quick DOS window flash but nothing runs.

Brett

Sep 12 '06 #3
Hi,

The code seems correct ,

do you see at least a shell window?
--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Brett Romero" <ac*****@cygen.comwrote in message
news:11**********************@i42g2000cwa.googlegr oups.com...
I'd like to execute a batch file. However, I just get a quick DOS
window flash when I run this code:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo
(@"C:\mybat.bat");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.Start();
proc.WaitForExit();

If I double click the bat file, I see the DOS window with output and
everything executes. If I open a DOS window and type in the bat file,
everything works fine again. The file modifies other files. Looking
at the files that are supposed to be modified, I can see it isn't
executing via the C# code.

Any ideas?

Thanks,
Brett

Sep 12 '06 #4

Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

The code seems correct ,

do you see at least a shell window?
No windows in other words. Just a flash but nothing stays around.

Brett

Sep 12 '06 #5
Brett Romero wrote:
Ignacio Machin ( .NET/ C# MVP ) wrote:
Hi,

The code seems correct ,

do you see at least a shell window?

No windows in other words. Just a flash but nothing stays around.

Brett

If you run your batch file manually, can it be invoked from anywhere?
Or must the command prompt be in a certain directory?

Try experimenting with the UseShellExecute and WorkingDirectory
properties of the ProcessStartInfo class. Perhaps setting them
appropriately will help.

Sep 12 '06 #6
Hi,

That is the expected behavior, the window stay while the program is
executing and disappear after it finish

try to put a ReadLine in the bat file

is it a short .bat ? if so post it here

--
--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation
"Brett Romero" <ac*****@cygen.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
>
Ignacio Machin ( .NET/ C# MVP ) wrote:
>Hi,

The code seems correct ,

do you see at least a shell window?

No windows in other words. Just a flash but nothing stays around.

Brett

Sep 12 '06 #7
If you run your batch file manually, can it be invoked from anywhere?
Or must the command prompt be in a certain directory?

Try experimenting with the UseShellExecute and WorkingDirectory
properties of the ProcessStartInfo class. Perhaps setting them
appropriately will help.
Ah! Thanks Chris. A little modification:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo( "mybat.bat" );
p.UseShellExecute = true;
p.WorkingDirectory = @"C:\";
p.WindowStyle = ProcessWindowStyle.Normal;

Thanks,
Brett

Sep 12 '06 #8

"Brett Romero" <ac*****@cygen.comwrote in message
news:11**********************@d34g2000cwd.googlegr oups.com...
|
| Ignacio Machin ( .NET/ C# MVP ) wrote:
| Hi,
| >
| The code seems correct ,
| >
| do you see at least a shell window?
|
| No windows in other words. Just a flash but nothing stays around.
|
| Brett
|

That means that the batch file gets started, right!, but questions are: what
are you doing in the bat file and why do you expect the cmd window to stay
arround?
Note that when one or another command in the bat fails, the command
interpreter will abort the curent bat file execution (unless you have some
error checking/recovery in place).

Willy.

Sep 12 '06 #9
Try adding a 'pause' DOS command at the end of your batch file.

It should block the proc.WaitForExit() and allow you to see whats going on
during execution.

--
Saad Rehmani / Prodika / Dallas / TX / USA
I'd like to execute a batch file. However, I just get a quick DOS
window flash when I run this code:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo
(@"C:\mybat.bat");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.Start();
proc.WaitForExit();
If I double click the bat file, I see the DOS window with output and
everything executes. If I open a DOS window and type in the bat file,
everything works fine again. The file modifies other files. Looking
at the files that are supposed to be modified, I can see it isn't
executing via the C# code.

Any ideas?

Thanks,
Brett

Sep 12 '06 #10
Is there a way to read the last three lines of output from the process
via StandardOutput or what ever is best?

Thanks,
Brett

Sep 12 '06 #11
Hi,

You can try out this code.

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo(@"C:\mybat.bat ");
p.UseShellExecute = true;
p.RedirectStandardOutput = true;
p.RedirectStandardError = true;

System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.Start();
proc.WaitForExit();

If you want to read the output from the console, can use this code as
well.
string output = proc.StandardOutput.ReadToEnd();

Cheers!!
Dipankar

Brett Romero wrote:
I'd like to execute a batch file. However, I just get a quick DOS
window flash when I run this code:

System.Diagnostics.ProcessStartInfo p = new
System.Diagnostics.ProcessStartInfo
(@"C:\mybat.bat");
System.Diagnostics.Process proc = new System.Diagnostics.Process();
proc.StartInfo = p;
proc.Start();
proc.WaitForExit();

If I double click the bat file, I see the DOS window with output and
everything executes. If I open a DOS window and type in the bat file,
everything works fine again. The file modifies other files. Looking
at the files that are supposed to be modified, I can see it isn't
executing via the C# code.

Any ideas?

Thanks,
Brett
Sep 13 '06 #12
Brett Romero wrote:
Is there a way to read the last three lines of output from the process
via StandardOutput or what ever is best?

Thanks,
Brett
Yes, you would have to set the RedirectStandardOutput property and then
open a stream against that to read what was output.

Sep 13 '06 #13

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1846
by: carrot | last post by:
With .Net Studio, I have a solution includes some projects. I want to execute a batch file during build solution time. For example, 1'st project is builded. 2'nd project is builded. " a batch...
1
15167
by: Rob | last post by:
I'm running a batch file using the Shell function. When I manually launch the batch file, the window remains open, since I use the 'pause' statement. But when I launch the batch file within...
4
47428
by: Bill | last post by:
I need help closing a CMD window when it is executed from Access. 1) The batch file is called from Access. 2) Access closes, 3) the batch runs a copy of the access database (creating a backup)...
4
2099
by: Jason Shohet | last post by:
A user runs a .NET application that authenticates them against active directory (they're coming in over VPN so they don't log on to the network normally). Once they log in, I want to take their...
4
2466
by: carrot | last post by:
With .Net Studio, I have a solution includes some projects. I want to execute a batch file during build solution time. For example, 1'st project is builded. 2'nd project is builded. " a batch...
13
5406
by: MLH | last post by:
I have a batch file named GetConf.bat. It contains a line like this: ipconfig /all >c:\MyAppDir\IPdata.txt I believe I could run the line with something like ShellWait by Terry Kreft. Any...
3
4432
by: emman_54 | last post by:
Hi every one, I am trying to run a batch file using my asp.net application. I am using the Process class to run the batch file. When I run my web application, In the task manager, i could see...
4
13363
by: ed | last post by:
Hi all, I'm very new to vb (2nd day) and I need to create a small app that will replace my old batch file with a flashy gui. I had some experience with access 2.0 which helps ;) What I would...
0
6181
by: Vijay Kumar | last post by:
Hi, I have a batch file(test.bat) i wrote a command to rename a text file in this batch file. Both batch file and text files are in Network Shared Drive. I am trying to run that batch file from...
7
2069
by: FireImp | last post by:
So I've read a lot of post about how to run a batch file from within the C# program. And I followed the instructions with a few alterations to fit my needs. First I am using cmd.exe to actually run...
0
7199
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7323
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7453
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5576
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
5005
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4670
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3151
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
732
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
377
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.