Ask the Expert

Redirecting results to a text file

I am programming ASPX pages in C# and I want to redirect the result from a created process to a text file, like:
 Process myProcess = new Process(); myProcess.StartInfo.FileName = "cmd.exe"; string a = @"/c echo Y| ping www.yahoo.com>d:temp.txt"; myProcess.StartInfo.Arguments = a; myProcess.StartInfo.UseShellExecute=false; myProcess.StartInfo.RedirectStandardOutput = true; myProcess.StartInfo.CreateNoWindow = true ; myProcess.Start();
But it did not work: nothing was written into the file temp.txt and I did not get any error message. Can you help me to find where the problem is?
The problem is a lack of permissions to write the target file, which is usually the case for the ASP.NET process identity, unless you increase permissions (i.e. creating an empty file in advance and giving ASP.NET or Everyone full control over it). In your case, I think it's better to avoid writing a file altogether. You can do that using the following code:
 ProcessStartInfo info = new ProcessStartInfo( "cmd.exe", @"/c echo Y| ping localhost"); info.UseShellExecute = false; info.RedirectStandardOutput = true; info.CreateNoWindow = true ; Process p = Process.Start(info); string output = p.StandardOutput.ReadToEnd(); Response.Write(output);
Writing files (fixed file names, specially) in an ASP.NET application is not a good idea as multiple threads can try to write the same file simultaneously.

More resources on ASPX files

ASP.NET C# database connection in external file

Setting permissions to write to event log

This was first published in November 2004

There are Comments. Add yours.

 
TIP: Want to include a code block in your comment? Use <pre> or <code> tags around the desired text. Ex: <code>insert code</code>

REGISTER or login:

Forgot Password?
By submitting you agree to receive email from TechTarget and its partners. If you reside outside of the United States, you consent to having your personal data transferred to and processed in the United States. Privacy
Sort by: OldestNewest

Forgot Password?

No problem! Submit your e-mail address below. We'll send you an email containing your password.

Your password has been sent to: