467,859 Members | 1,361 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,859 developers. It's quick & easy.

batch files in visual c# windows application

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
Nov 17 '05 #1
  • viewed: 10850
Share:
6 Replies
Try

Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/c" + setPath("tuneup.bat");
myProcess.Start();
"Charles Neitzel" wrote:
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

Nov 17 '05 #2
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.

"tony lock" wrote:
Try

Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/c" + setPath("tuneup.bat");
myProcess.Start();
"Charles Neitzel" wrote:
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

Nov 17 '05 #3
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

"tony lock" wrote:
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.

"tony lock" wrote:
Try

Process myProcess = new Process();
myProcess.StartInfo.FileName = "cmd.exe";
myProcess.StartInfo.Arguments = "/c" + setPath("tuneup.bat");
myProcess.Start();
"Charles Neitzel" wrote:
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

Nov 17 '05 #4
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

"Charles Neitzel" wrote:
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

"tony lock" wrote:
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.

"tony lock" wrote:
Try

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

> 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

Nov 17 '05 #5
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.

"Charles Neitzel" wrote:
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

"Charles Neitzel" wrote:
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

"tony lock" wrote:
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.

"tony lock" wrote:

> Try
>
> Process myProcess = new Process();
> myProcess.StartInfo.FileName = "cmd.exe";
> myProcess.StartInfo.Arguments = "/c" + setPath("tuneup.bat");
> myProcess.Start();
>
>
> "Charles Neitzel" wrote:
>
> > 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

Nov 17 '05 #6
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");

"Charles Neitzel" wrote:
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

"Charles Neitzel" wrote:
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

"tony lock" wrote:
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.

"tony lock" wrote:

> Try
>
> Process myProcess = new Process();
> myProcess.StartInfo.FileName = "cmd.exe";
> myProcess.StartInfo.Arguments = "/c" + setPath("tuneup.bat");
> myProcess.Start();
>
>
> "Charles Neitzel" wrote:
>
> > 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

Nov 17 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by Herb Stevenson | last post: by
7 posts views Thread by erniedude | last post: by
3 posts views Thread by emman_54 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.