2

I am trying to output the result of the openfiles process to a file but I am not getting any result can someone explain why? I have tried it 2 different ways. If I use the command prompt and run the same process it does display a result to my file.

Update: I added a third method with the change redirectstandardoutput = true I tried this and now I get a file but with no results

Update: I found the problem it is with the build option being set to x86 when doing this on a 64bit system I think it is running the 32bit version of openfiles. I tested by running my app and using the RedirectStandardError stream which I should have been doing in the first place :) and this is what it said "ERROR: The target system must be running a 32 bit OS."

//First Method
using (Process proc = new Process())
{
  proc.StartInfo.UseShellExecute = false;
  proc.StartInfo.CreateNoWindow = true;
  proc.StartInfo.FileName = "openfiles";
  proc.StartInfo.Arguments = "/query /FO CSV /v > " + "\"" + Application.StartupPath + @"\OpenFiles.log" + "\"";
  proc.Start();
}

//Second method
using (Process proc = new Process())
{
   proc.StartInfo.UseShellExecute = false;
   proc.StartInfo.CreateNoWindow = true;
   proc.StartInfo.FileName = "cmd";
   proc.StartInfo.Arguments = "/C openfiles /query /FO CSV /v > " + "\"" + Application.StartupPath + @"\OpenFiles.log" + "\"";
   proc.Start();
 }

//Third method
using (Process proc = new Process())
{
   proc.StartInfo.UseShellExecute = false;
   proc.StartInfo.CreateNoWindow = true;
   proc.StartInfo.RedirectStandardOutput = true;
   proc.StartInfo.FileName = "openfiles";
   proc.StartInfo.Arguments = "/query /FO CSV /v";
   proc.Start();
   string output = proc.StandardOutput.ReadToEnd();
   proc.WaitForExit();
   if (output != null)
   File.WriteAllText(Path.Combine(Application.StartupPath, "OpenFiles.log"), output);
 }
  • possible duplicate of [redirecting output to the text file c#](http://stackoverflow.com/questions/16256587/redirecting-output-to-the-text-file-c-sharp) – huysentruitw Sep 24 '14 at 17:29

1 Answers1

0

The redirect will not work like that as > filename is not seen as an 'argument'.

Typically, you will capture the output in your application and write it to a file yourself:

proc.StartInfo.RedirectStandardOutput = true;
proc.Start();
string output = proc.StandardOutput.ReadToEnd();
proc.WaitForExit();

File.WriteAllText(Path.Combine(Application.StartupPath, "OpenFiles.log"), output);
huysentruitw
  • 27,376
  • 9
  • 90
  • 133
  • 1
    I did see other questions/answers about re-directing the standard output but when I tried that it also did not work. Let me try again I will update with what I find using the standard output. Thanks all – Ryan Mathes Sep 24 '14 at 17:38
  • thank you for the answer but for some reason this does not work for me, I did run another project which is on codeproject doing the same exact thing I am and his code works if I run his project so I am just puzzled why mine does not :( – Ryan Mathes Sep 24 '14 at 20:47
  • 1
    I found the problem it is with the build option being set to x86 when doing this on a 64bit system I think it is running the 32bit version of openfiles. I tested by running my app and using the RedirectStandardError stream which I should have been doing in the first place :) and this is what it said "ERROR: The target system must be running a 32 bit OS." – Ryan Mathes Sep 24 '14 at 21:20
  • hehe, glad you've found the issue! – huysentruitw Sep 25 '14 at 07:51