Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

batch files in visual c# windows application

8 views
Skip to first unread message

Neitzel@discussions.microsoft.com Charles Neitzel

unread,
Oct 9, 2005, 5:19:01 PM10/9/05
to
I'm trying to write a windows application in C# (Using Microsoft Visual C#
2005 Express) that is nothing more than a simple UI with buttons on it. The
buttons do various things like running programs and executing registry
entries. The majority of my buttons work however, I have come upon a problem.
I need a few of the buttons to run DOS batch files, the batch files in turn
run program installers (specifically windows update runtime .exe files). The
batch files work the way I want them to when I execute them in windows,
however when I use my buttons in my C# program to run the batch files, they
run, but when the batch file that opens tries to run the installers, I get
errors for everything the batch file tries to run, be it installers, other
batch files, registry entries, etc. It appears as if it isn't opening an
actual batch file, but a C# console containing the code from the batch file.
The errors come out like this - KB896423.exe is the name of the file it is
trying to run:
"KB896423.exe is not recognized as an internal or external command, operable
program or batch file."
As I stated before, these batch files do work as long as they are executed
directly from windows...I just want my program to do it for me. I've searched
for days for help on this and can't find a solution - is this even possible?

Also, assuming it is indeed possible to do that, is there a way I can hide
the batch file window that opens so it just runs in the background? I've
messed around for hours with the System.Diagnostics.ProcessWindowStyle.Hidden
(and .Minimized) code but have determined that it must only be designed to
work with C# console applications and not windows applications.

Here is the code I'm using to run my batch files with the Click function of
my buttons at the moment, but I've tried many ways to do it:

/*Runs Tuneup.bat file*/

private void tuneupbtn_Click(object sender, EventArgs e)
{
Process.Start(setpath("tuneup.bat"));
}

the ..setpath("tuneup.bat").. portion of that is a method which comes from
another part of my program which sets the path of the file, regardless of
what the drive letter is. I had to do it this way because the program is
going to be ran from a USB flash drive so the drive letter will change
depending on the number of hard disks/partitions are there.

That being said - the button will run the batch file...problem is it tries
to run it as a C# console application (when it's written as a standard DOS
batch file) rather than using the windows shell, hence, it won't work.

Am I trying to do something that isn't possible? Am I going to have to
rewrite my batch files to run as a c# console application? Let me know if
anybody can help, thanks.

Charles Neitzel

tony lock

unread,
Oct 10, 2005, 7:59:06 AM10/10/05
to
Try

Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/c" + setPath("tuneup.bat");
myProcess.Start();

tony lock

unread,
Oct 10, 2005, 8:09:04 AM10/10/05
to
I did not notice the bit about the window not showing, to hide the command
window add

myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

before the myProcess.Start(); this will had the command window but not what
ever is generated by the bat file.

Charles Neitzel

unread,
Oct 10, 2005, 12:56:03 PM10/10/05
to
I appreciate your time in responding to my questions. I have tried doing it
in a very similar way to that in the past and it hasn't seemed to work but
I'll give it a shot just the way you typed it and see how it works - I'll let
you know the outcome. Thanks.

Charles

Charles Neitzel

unread,
Oct 10, 2005, 1:18:04 PM10/10/05
to
Ok so I'm making progress with that suggestion. I can get it to open the cmd
window at the appropriate directory, but it won't actually run the batch
file, it just disappears immediately (even after changing the
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden; to
myProcess.StartInfo.WindowStyle = ProcessWindowStyle.Normal;

I've tried putting in another argument line like this:

myProcess.StartInfo.Arguments = "tuneup.bat";

Putting that in there will make it so the window doesn't disappear and I can
actually use the command line. I can type tuneup.bat in there and it will
run the file just like it's supposed to, so the next problem comes in making
the program run it for me. Again I appreciate the help, I'm going to keep
messing with it to see if I can get it to work right. What am I missing?

Charles

tony lock

unread,
Oct 10, 2005, 6:54:07 PM10/10/05
to
The /c before the argument is important, it tells cmd.exe to to run the batch
file and then close, you cannot omit it. If you do as you found out you have
to type the command yourself, I do not know what's in your batch file. But a
simple file containing calc.exe as a command, certainly works in the way you
require, using the code I gave you. The calculator starts with out showing
the dos box and everything terminates when the bat file closes.

tony lock

unread,
Oct 10, 2005, 6:57:05 PM10/10/05
to
Sorry just realised in copying the code into the note I left out the space
after the c it should read

myProcess.StartInfo.Arguments = "/c " + setPath("tuneup.bat");

0 new messages