This is not deleting the file.txtCode:System.Diagnostics.Process.Start("cmd.exe", @"/c del d:\\file.txt");
Printable View
This is not deleting the file.txtCode:System.Diagnostics.Process.Start("cmd.exe", @"/c del d:\\file.txt");
You're using the "@" to tell C# that the strings are unescaped. If you remove the double \ in the path it should work.
However, you know C# has it's own file functions right? It can delete files without having to resort to using the command prompt.
TryCode:System.IO.File.Delete("d:\\file.txt")
This is what my complete code is
I am getting error in the stream reader(filepath)Code:System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
StreamReader streamReader = new StreamReader(filePath);
string readtext = streamReader.ReadToEnd();
streamReader.Close();
System.IO.File.Delete("d:\file.txt"); //Delete the output dump file
Code:Could not find file 'd:\file.txt'..
Hey,
Ok, I have to ask. Why are you creating the file, and then straight away deleting it? Or does the above post miss out some other steps?
Have you verified that the file does get created?
You should probably use this:
http://msdn.microsoft.com/en-us/libr...le.exists.aspx
To verify whether the file is there before trying to delete it.
Gary
I am running a command ie route print and directing it to a file named file.txt. then i am reading the content to a string name readtext. Is there a way to redirect the command output to a string rather than to a file and reading from the file.
once i get the text to a string, i dont need the text file. So i am deleting it
Here is what i have as code
i think by introducing a delay , the problem will be solvedCode:System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
Thread.Sleep(500);
using (StreamReader streamReader = File.OpenText(filePath))
{
string s = "";
if ((s = streamReader.ReadLine()) != null)
{
readtext = streamReader.ReadToEnd();// streamReader.ReadToEnd();
}
}
/* delete all unwanted files */
System.IO.File.Delete("d:\\file.txt"); //Delete the output dump file
Hey,
Where is filepath defined then? And why are you not using it in the Process.Start method?
For your second question, you might want to take a look here:
http://www.vbforums.com/showthread.php?t=381405
Gary
i forget to add the filepath in the pasted code
Code:string filePath = @"d:\file.txt";
string readtext = "";
System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
Thread.Sleep(500);
using (StreamReader streamReader = File.OpenText(filePath))
{
string s = "";
if ((s = streamReader.ReadLine()) != null)
{
readtext = streamReader.ReadToEnd();// streamReader.ReadToEnd();
}
}
Hey,
Okay, so why not reuse that variable everywhere that is needed, rather than re-typing it.
Did you look at the link that I posted?
From what you have described, it does everything that you need, and doesn't require you to do the above.
Gary
Hey,
Why exactly did it fail? What error did you get?
Can you post what you used?
Gary
Hey,
In fact, here is a knocked up version of the same code in C#:
I think the issue that you would have been having was the updating of the TextBox Text from another thread. I have solved this using a delegate method to perform the update. For more information on what exactly I did, have a look here:Code:private void button1_Click(object sender, EventArgs e)
{
Thread cmdThread = new Thread(CMDAutomate);
cmdThread.Start();
}
private void CMDAutomate()
{
Process myprocess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd";
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
myprocess.StartInfo = startInfo;
myprocess.Start();
StreamReader sr = myprocess.StandardOutput;
StreamWriter sw = myprocess.StandardInput;
sw.WriteLine(textBox1.Text);
sw.WriteLine("exit");
SetTextBoxText(sr.ReadToEnd());
sw.Close();
sr.Close();
}
private delegate void SetTextBoxTextInvoker(string text);
private void SetTextBoxText(string text)
{
if (this.textBox2.InvokeRequired)
{
this.textBox2.Invoke(new SetTextBoxTextInvoker(SetTextBoxText),text);
}
else
{
this.textBox2.Text = text;
}
}
http://www.vbforums.com/showthread.php?t=544824
Gary
here are the errors;Code:System.Diagnostics.ProcessStartInfo StartInfo;
StartInfo.FileName = "cmd" ; //
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.UseShellExecute = false; //'required to redirect
StartInfo.CreateNoWindow = true; // '<---- creates no window, obviously
myprocess.StartInfo = StartInfo; //
myprocess.Start(); //
System.IO.StreamReader SR;
System.IO.StreamWriter SW;
SR = myprocess.StandardOutput;
SW=myprocess.StandardInput;
SW.WriteLine("route print > d:\file.txt"); // 'the command you wish to run.....
SW.WriteLine("exit"); // 'exits command prompt window
readtext = SR.ReadToEnd;
SW.Close();
SR.Close();
Code:1 .Error The name 'myprocess' does not exist in the current context
2. Error 5 Cannot convert method group 'ReadToEnd' to non-delegate type 'string'. Did you intend to invoke the method?
Hey,
You are missing this line:
Also, you need to do this:Code:Process myprocess = new Process();
ReadToEnd is a method, so you need the parentheses. Visual Basic is a less stringent on this matter.Code:readtext = SR.ReadToEnd();
Why are you still piping the output to a text file? You shouldn't need this anymore?!?
Gary
i need the values obatined from this read to a text file because later stage , i want to get some information from this command run. The command is to find which all are the current network connections in the system. Thats the reason , it is redirected to a fileCode:Error 1 The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)
Hey,
You are missing a using statement at the top of your code file:
Either that, or change it to this:Code:using System.Diagnostics;
I understand what the command is doing, but earlier you said that all you want to do is read the output into the string variable, and then delete the file. This is certainly what you were doing in post #4. Using the approach that you are now, the output is being read directly into the string variable, so I don't see why you still need the file. But it's up to you. You may still have the problem with deleting the file.Code:System.Diagnostics.Process myprocess = new System.Diagnostics.Process();
Gary
errorCode:ProcessStartInfo startInfo = new ProcessStartInfo();
Process myprocess = new Process();
StartInfo.FileName = "cmd"; //
StartInfo.RedirectStandardInput = true;
StartInfo.RedirectStandardOutput = true;
StartInfo.UseShellExecute = false; //'required to redirect
StartInfo.CreateNoWindow = true; // '<---- creates no window, obviously
myprocess.StartInfo = StartInfo; //
myprocess.Start(); //
System.IO.StreamReader SR;
System.IO.StreamWriter SW;
SR = myprocess.StandardOutput;
SW = myprocess.StandardInput;
SW.WriteLine("route print > d:\file.txt"); // 'the command you wish to run.....
SW.WriteLine("exit"); // 'exits command prompt window
readtext = SR.ReadToEnd();
SW.Close();
SR.Close();
}
If instead of writiing to a file and reding to a string, if i can get it straight to the text, i have no problem.Code:Error 1 Use of unassigned local variable 'StartInfo'
so if i modify the above to
will this help me .Code:SW.WriteLine("route print"); // 'the command you wish to run.....
SW.WriteLine("exit"); // 'exits command prompt window
readtext = SR.ReadToEnd();
Hey,
You have to remember that C# is a case sensitive language, so a variable called startInfo is not the same as one called StartInfo. Pick one, and stick with it. I would recommend startInfo.
Yip, I would say that is the best approach for you.
Gary
Oh Thanks for that info.
Some clarification is needed.
i was expecting the output of route print command in the read text. But it is not. I am sure i have missed somethingCode:SW.WriteLine("route print"); // 'the command you wish to run.....
SW.WriteLine("exit"); // 'exits command prompt window
readtext = SR.ReadToEnd();
SW.Close();
SR.Close();
Hey,
Have you tried stepping through your code in the debugger?
I have just tried it in the sample application that I have here, and it works fine for me.
Can you post all the code that you are using?
Gary
Step thriugh was working not on run, So i modified with including a sleep time
This is running. A time delay was needed to write to the text file and then read from thereCode:SW.WriteLine("route print > d:\\file.txt"); // 'the command you wish to run.....
SW.WriteLine("exit"); // 'exits command prompt window
Thread.Sleep(1000);
using ( SR = File.OpenText(filePath))
{
readtext = SR.ReadToEnd();
}
SW.Close();
No, that is definitely not the right approach.
You should not have to do this at all. Adding in Thread.Sleep's is not a sensible approach at all.
Why have you given up on loading it directly in the string variable. It works perfectly:
GaryCode:private void button2_Click(object sender, EventArgs e)
{
string readText = string.Empty;
Process myprocess = new Process();
ProcessStartInfo startInfo = new ProcessStartInfo();
startInfo.FileName = "cmd";
startInfo.RedirectStandardInput = true;
startInfo.RedirectStandardOutput = true;
startInfo.UseShellExecute = false;
myprocess.StartInfo = startInfo;
myprocess.Start();
StreamReader sr = myprocess.StandardOutput;
StreamWriter sw = myprocess.StandardInput;
sw.WriteLine("route print");
sw.WriteLine("exit");
readText = sr.ReadToEnd();
sw.Close();
sr.Close();
MessageBox.Show(readText);
}
That was a mistake in the data displayed vs what i read . There were some additional data coming likewhich was not coming when dumping to a text file . Thanks for pointiing out the mistakeCode:Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
Hey,
That is because this command:
Is piping just the output of the command, where as, with redirecting the command windows standard output, you are getting all the text. Hit Start | run, type cmd and hit enter, and you will see what I mean.Code:route print > d:\\file.txt
You should be able to extract just the information that you want.
Gary
Great ,
Thanks for the info. closing this thread.
Got lot of info
Thanks Gary
Hey,
Not a problem at all, happy to help!
Gary