473,419 Members | 1,795 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

.bat from asp.net w/System.Diagnostics.Process?

I need to be able to execute a .bat file from a C# web application
I have the following code that compliles and seems to run fine, but the bat file never does it's work
===================================
System.Diagnostics.Process p = new Process()
p.StartInfo.RedirectStandardOutput=false
p.StartInfo.FileName = "C:\\ftp_scripts\\script.bat"
p.StartInfo.UseShellExecute=true
p.Start()
p.WaitForExit()
p.Dispose();
====================================
The bat file works fine when i don't call it from the web app

What could be wrong?
Is it a permissions thing?
Why am I not getting any errors?
How do I get errors back to my calling code

Any help would be greatly appreciated
-Dan
Nov 18 '05 #1
6 2176
how do you know the bat file is not working? Are you testing the results of
what the bat file executed to know this? You cannot run the application and
look for a bat file window. If you insist that the bat file is not firing
off, here is another way to invoke it using the command line modifying your
code.

System.Diagnostics.Process p = new Process();
// p.StartInfo.RedirectStandardOutput=false;
p.StartInfo.RedirectStandardOutput=true;
p.StartInfo.RedirectStandardInput=true;

p.StartInfo.FileName = @"cmd"; //"C:\\ftp_scripts\\script.bat";
// p.StartInfo.UseShellExecute=true;
p.StartInfo.UseShellExecute=false;

p.Start();
// p.WaitForExit();
// p.Dispose();

StreamWriter sIn = myProcess.StandardInput;
sIn.AutoFlush = true;
StreamReader sOut = myProcess.StandardOutput;

sIn.Write(@"C:\ftp_scripts\script.bat" + System.Environment.NewLine);
sIn.Close();

string results = sOut.ReadToEnd().Trim();
p.Close();

The results of the script, whether it failed to run or errored out will be
contained in results variable so you can know exactly what is going on.
--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
<danl@drawnet_no_spam.com> wrote in message
news:21**********************************@microsof t.com...
I need to be able to execute a .bat file from a C# web application.
I have the following code that compliles and seems to run fine, but the
bat file never does it's work.
====================================
System.Diagnostics.Process p = new Process();
p.StartInfo.RedirectStandardOutput=false;
p.StartInfo.FileName = "C:\\ftp_scripts\\script.bat";
p.StartInfo.UseShellExecute=true;
p.Start();
p.WaitForExit();
p.Dispose();
=====================================
The bat file works fine when i don't call it from the web app.

What could be wrong?
Is it a permissions thing?
Why am I not getting any errors?
How do I get errors back to my calling code?

Any help would be greatly appreciated.
-Dan

Nov 18 '05 #2
Thanks for your help Alvin, but I still can't get the sript to run. I tried to simplify the code by executing the command that was in the bat file directly from this script, still no luck.

The cmd simply takes a file and ftp's it to another server. the output is recorded in a log file. If I execute the cmd from the cmd line, it works fine (the file is moved, and there is an entry in the log file)

When I execute it with the following script from an .aspx, no file moved and no log entry.

I'm stumped!! Any other idea's why this might not be working

======================================
System.Diagnostics.Process p = new Process()
p.StartInfo.RedirectStandardOutput=true
p.StartInfo.RedirectStandardInput=true
p.StartInfo.FileName = @"cmd"
p.StartInfo.UseShellExecute=false
p.Start()
StreamWriter sIn = p.StandardInput
sIn.AutoFlush = true
StreamReader sOut = p.StandardOutput

sIn.Write(@"ftp -s:c:\ftp_scripts\ftp.txt >> c:\ftp_scripts\ftp_log.txt" + System.Environment.NewLine)
sIn.Close()

string results = sOut.ReadToEnd().Trim()
p.Close()

Response.Write(results)
==========================================

OUTPU
==========================================
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\WINDOWS\system32>ftp -s:c:\ftp_scripts\ftp.txt >> c:\ftp_scripts\dbftplog.txt C:\WINDOWS\system32>
===========================================
Nov 18 '05 #3
what is the results of the script from the command prompt when you run using
a webpage?
i'm not familiar with the syntax of the ftp command you are using. from the
docs, ftp takes a verb followed by command line options. Post your script
code if you still have problems.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
<danl@drawnet_no_spam.com> wrote in message
news:58**********************************@microsof t.com...
Thanks for your help Alvin, but I still can't get the sript to run. I
tried to simplify the code by executing the command that was in the bat
file directly from this script, still no luck.

The cmd simply takes a file and ftp's it to another server. the output is
recorded in a log file. If I execute the cmd from the cmd line, it works
fine (the file is moved, and there is an entry in the log file).

When I execute it with the following script from an .aspx, no file moved
and no log entry.

I'm stumped!! Any other idea's why this might not be working?

=======================================
System.Diagnostics.Process p = new Process();
p.StartInfo.RedirectStandardOutput=true;
p.StartInfo.RedirectStandardInput=true;
p.StartInfo.FileName = @"cmd";
p.StartInfo.UseShellExecute=false;
p.Start();
StreamWriter sIn = p.StandardInput;
sIn.AutoFlush = true;
StreamReader sOut = p.StandardOutput;

sIn.Write(@"ftp -s:c:\ftp_scripts\ftp.txt >> c:\ftp_scripts\ftp_log.txt" +
System.Environment.NewLine);
sIn.Close();

string results = sOut.ReadToEnd().Trim();
p.Close();

Response.Write(results);
===========================================

OUTPUT
===========================================
Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft
Corp. C:\WINDOWS\system32>ftp -s:c:\ftp_scripts\ftp.txt >>
c:\ftp_scripts\dbftplog.txt C:\WINDOWS\system32>
===========================================

Nov 18 '05 #4
Great, so here is where you start. After each line in your script, echo it
to the screen. Then run the app. Just follow the screen output to find which
line is failing.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
<danl@drawnet_no_spam.com> wrote in message
news:E7**********************************@microsof t.com...
The results of the script from the command prompt when run from an .aspx
are
====================
> Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001

Microsoft
> Corp. C:\WINDOWS\system32>ftp -s:c:\ftp_scripts\ftp.txt >>>

c:\ftp_scripts\dbftplog.txt C:\WINDOWS\system32>
====================
The ftp call syntax that I am using allows you to have a text file with
all of your ftp connection info in it. The contents of the
c:\ftp_scripts\ftp.txt file is
===================
open 11.22.33.44
uname
pword
cd mydir
put "C:\SQLData\MSSQL\BACKUP\test.txt"
quit
===================

I'd really like to figure out why my code won't run from a web page, but I
really just need to be able to ftp from an .aspx page. If you know of a
better way, I'm open to anything that works.

Thanks for your help.
-Dan
----- Alvin Bruney [MVP] wrote: -----

what is the results of the script from the command prompt when you run
using
a webpage?
i'm not familiar with the syntax of the ftp command you are using.
from the
docs, ftp takes a verb followed by command line options. Post your
script
code if you still have problems.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
<danl@drawnet_no_spam.com> wrote in message
news:58**********************************@microsof t.com...
> Thanks for your help Alvin, but I still can't get the sript to run.

I
> tried to simplify the code by executing the command that was in the

bat
> file directly from this script, still no luck.
>> The cmd simply takes a file and ftp's it to another server. the

output is
> recorded in a log file. If I execute the cmd from the cmd line, it

works
> fine (the file is moved, and there is an entry in the log file).
>> When I execute it with the following script from an .aspx, no file

moved
> and no log entry.
>> I'm stumped!! Any other idea's why this might not be working?
>> =======================================

> System.Diagnostics.Process p = new Process();
> p.StartInfo.RedirectStandardOutput=true;
> p.StartInfo.RedirectStandardInput=true;
> p.StartInfo.FileName = @"cmd";
> p.StartInfo.UseShellExecute=false;
> p.Start();
> StreamWriter sIn = p.StandardInput;
> sIn.AutoFlush = true;
> StreamReader sOut = p.StandardOutput;
>> sIn.Write(@"ftp -s:c:\ftp_scripts\ftp.txt >>

c:\ftp_scripts\ftp_log.txt" +
> System.Environment.NewLine);
> sIn.Close();
>> string results = sOut.ReadToEnd().Trim();

> p.Close();
>> Response.Write(results);

> ===========================================
>> OUTPUT

> ===========================================
> Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001

Microsoft
> Corp. C:\WINDOWS\system32>ftp -s:c:\ftp_scripts\ftp.txt >>>

c:\ftp_scripts\dbftplog.txt C:\WINDOWS\system32>>
===========================================

Nov 18 '05 #5
ok, I think I've found a problem, but I'm not sure why it's happening

If I use the command prompt, from "C:\Windows\system32", if i type in ftp and hit enter the next line I get is "ftp>", I can then enter open 11.22.33.44 and it promts me for my uname.... and I'm on my way

When I execute the following code from the .aspx, the output shows
"Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\WINDOWS\system32>ftp C:\WINDOWS\system32>".

Where I would have expected
"Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\WINDOWS\system32>ftp ftp>"
-----------------------------------------------------------------
System.Diagnostics.Process p = new Process()
p.StartInfo.RedirectStandardOutput=true
p.StartInfo.RedirectStandardInput=true
p.StartInfo.FileName = @"cmd"
p.StartInfo.UseShellExecute=false
p.Start()
StreamWriter sIn = p.StandardInput
sIn.AutoFlush = true
StreamReader sOut = p.StandardOutput
sIn.Write(@"ftp" + System.Environment.NewLine)
//sIn.Write(@"open 11.22.33.44" + System.Environment.NewLine)
//sIn.Write(@"myuname" + System.Environment.NewLine)
//sIn.Write(@"mypword" + System.Environment.NewLine)
//sIn.Write(@"cd dbdump" + System.Environment.NewLine)
//sIn.Write(@"put ""C:\SQLData\MSSQL\BACKUP\test9.txt""" + System.Environment.NewLine)
sIn.Write(@"quit" + System.Environment.NewLine)
sIn.Close()
string results = sOut.ReadToEnd().Trim()
p.Close()
Response.Write(results)
----------------------------------------------------------------
(NOTE that the commands after ftp are commented out

Why from the .aspx page is it going back to
C:\WINDOWS\system32
and not to
ftp
??
Nov 18 '05 #6
you may have to put a "ftp" followed by a newline command so that it is
sitting in the ftp prompt before you start firing your command line params.
That should fix it.

--
Regards,
Alvin Bruney [ASP.NET MVP]
Got tidbits? Get it here...
http://tinyurl.com/27cok
<danl@drawnet_no_spam.com> wrote in message
news:CF**********************************@microsof t.com...
ok, I think I've found a problem, but I'm not sure why it's happening.

If I use the command prompt, from "C:\Windows\system32", if i type in ftp
and hit enter the next line I get is "ftp>", I can then enter open
11.22.33.44 and it promts me for my uname.... and I'm on my way.

When I execute the following code from the .aspx, the output shows:
"Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft
Corp. C:\WINDOWS\system32>ftp C:\WINDOWS\system32>".

Where I would have expected:
"Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft
Corp. C:\WINDOWS\system32>ftp ftp>".
------------------------------------------------------------------
System.Diagnostics.Process p = new Process();
p.StartInfo.RedirectStandardOutput=true;
p.StartInfo.RedirectStandardInput=true;
p.StartInfo.FileName = @"cmd";
p.StartInfo.UseShellExecute=false;
p.Start();
StreamWriter sIn = p.StandardInput;
sIn.AutoFlush = true;
StreamReader sOut = p.StandardOutput;
sIn.Write(@"ftp" + System.Environment.NewLine);
//sIn.Write(@"open 11.22.33.44" + System.Environment.NewLine);
//sIn.Write(@"myuname" + System.Environment.NewLine);
//sIn.Write(@"mypword" + System.Environment.NewLine);
//sIn.Write(@"cd dbdump" + System.Environment.NewLine);
//sIn.Write(@"put ""C:\SQLData\MSSQL\BACKUP\test9.txt""" +
System.Environment.NewLine);
sIn.Write(@"quit" + System.Environment.NewLine);
sIn.Close();
string results = sOut.ReadToEnd().Trim();
p.Close();
Response.Write(results);
-----------------------------------------------------------------
(NOTE that the commands after ftp are commented out)

Why from the .aspx page is it going back to:
C:\WINDOWS\system32>
and not to:
ftp>
??

Nov 18 '05 #7

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

Similar topics

0
by: marccruz | last post by:
Given an instance of System.Diagnostics.Process, how can I get the parent process o Given an instance of System.Diagnostics.Process, how can I get the child processes For example, I start a...
1
by: Heiko Besemann | last post by:
Dear group, I created an aspx page using System.Diagnostics.Process() to launch "tracert" from shell and redirect output to Response.Output which prints it as text/plain into my browsers window....
1
by: solex | last post by:
Hello All, Hopefully someone has run into this error. I have written a class(source below) that launches a thread to monitor the StandardOutput of a System.Diagnostics.Process, in particular I...
2
by: andreas | last post by:
hi, In windows xp in the start launch menu when i put notepad "c:\test.txt" i get notepad with test.txt in it. in vb.net when i state system.diagnostics.process.start("notepad.exe" i get...
11
by: Nurit N | last post by:
This is the third newsgroup that I'm posting my problem. I'm sorry for the multiple posts but the matter becoming urgent. I hope this is the right place for it... I have created a very...
0
by: Daniel | last post by:
System.Diagnostics.Process.Start fails on windows server 2003 the process returns process.ExitCode == 0 but executing any process with System.Diagnostics.Process.Start on windows xp works fine....
0
by: Daniel | last post by:
C# windows service freezes on System.Diagnostics.Process.Start(info) When I launch PSCP from a C# windows service and launch pscp 0.53 there are no issues. but when I use C# windows service to...
0
by: Colin Williams | last post by:
I am using the code below to map network drive and then fire up an app in a sub dir of that drive. However when using the file open dialog from that app, drive K: appears just as Network drive K:...
0
by: =?Utf-8?B?UHVjY2E=?= | last post by:
-- Hi I'm using vs2005, .net 2 for windows application. The application I started using System.Diagnostics.Process is having a "listFiles.StandardOutput" buffer size problem. I was wondering...
2
by: test3 | last post by:
Hello folks, I'm using System.Diagnostics.Process to start a thirdparty program (that works perfectly when started via command line). I'm using Process.StandardOutput to get the output of the...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
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
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...

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.