I am invoking Powershell from C# to gather some information and using Out-File to send it to a text file. Then I need to read the lines from said file and do stuff with the data inside of C#.
string MyCommand = "-Command &{ get-process | Out-File C:\\MyFile.txt}";
ProcessStartInfo MyProcInfo = new ProcessStartInfo();
MyProcInfo.FileName = "powershell.exe";
MyProcInfo.Arguments = MyCommand;
Process MyProcess = new Process();
MyProcess.StartInfo = MyProcInfo;
MyProcess.Start();
MyProcess.WaitForExit();
try
{
var lines = File.ReadLines(@"C:\MyFile.txt");
(etc)
}
catch (Exception Ex)
{
MessageBox.Show(Ex.ToString());
}
So when it tries to open the text file I am getting a
"File Not Found"
exception. The file IS being written every time, so I am assuming that there is a timing thing going on which is why I am using WaitForExit. But it is still not able to 'find' the file.
C:and the file is written? Because of theUnauthorizedAccessExceptionthat is thrown when writing directly toC:, I doubt it. UseMyProcInfo.UseShellExecute = false; MyProcInfo.RedirectStandardOutput = true;, then redirect that output to astringand see where the problem is.while(!MyProcess.StandardOutput.EndOfStream)that enclosesMyProcess.StandardOutput.ReadLine();should do the trick.%localappdata%\VirtualStore. If the file is there when viewed from explorer, what are the file permissions on the file?