problem with batch files in my C# windows application

C

Charles

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
 
N

Nicholas Paldino [.NET/C# MVP]

Charles,

Create a ProcessStartInfo instance with the path of the batch file to
run and whatnot. Also, set the UseShellExecute property on the
ProcessStartInfo instance to true, and the shell should process the batch
file, and it should work.

Hope this helps.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top