|
-
Jan 6th, 2010, 05:49 AM
#1
Thread Starter
Addicted Member
[RESOLVED] Whats wrong with system.diagnostics
Code:
System.Diagnostics.Process.Start("cmd.exe", @"/c del d:\\file.txt");
This is not deleting the file.txt
If you found my reply helpful, please rate me

-
Jan 6th, 2010, 06:36 AM
#2
Re: Whats wrong with system.diagnostics
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.
-
Jan 6th, 2010, 09:11 AM
#3
Re: Whats wrong with system.diagnostics
Try
Code:
System.IO.File.Delete("d:\\file.txt")
-
Jan 7th, 2010, 01:17 AM
#4
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
This is what my complete code is
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
I am getting error in the stream reader(filepath)
Code:
Could not find file 'd:\file.txt'..
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 03:06 AM
#5
Re: Whats wrong with system.diagnostics
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
-
Jan 7th, 2010, 03:28 AM
#6
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
 Originally Posted by gep13
Hey,
Why are you creating the file, and then straight away deleting it? Or does the above post miss out some other steps?
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
Code:
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
i think by introducing a delay , the problem will be solved
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 03:30 AM
#7
Re: Whats wrong with system.diagnostics
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
-
Jan 7th, 2010, 03:38 AM
#8
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
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();
}
}
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 03:53 AM
#9
Re: Whats wrong with system.diagnostics
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
-
Jan 7th, 2010, 04:16 AM
#10
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
 Originally Posted by gep13
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
I tried with that code . Tried to make it work in C#. But failed. So still experimenting with that
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 04:17 AM
#11
Re: Whats wrong with system.diagnostics
Hey,
Why exactly did it fail? What error did you get?
Can you post what you used?
Gary
-
Jan 7th, 2010, 04:30 AM
#12
Re: Whats wrong with system.diagnostics
Hey,
In fact, here is a knocked up version of the same code in C#:
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;
}
}
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:
http://www.vbforums.com/showthread.php?t=544824
Gary
-
Jan 7th, 2010, 04:49 AM
#13
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
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();
here are the errors;
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?
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 04:52 AM
#14
Re: Whats wrong with system.diagnostics
Hey,
You are missing this line:
Code:
Process myprocess = new Process();
Also, you need to do this:
Code:
readtext = SR.ReadToEnd();
ReadToEnd is a method, so you need the parentheses. Visual Basic is a less stringent on this matter.
Why are you still piping the output to a text file? You shouldn't need this anymore?!?
Gary
-
Jan 7th, 2010, 05:00 AM
#15
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
Code:
Error 1 The type or namespace name 'Process' could not be found (are you missing a using directive or an assembly reference?)
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 file
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 05:05 AM
#16
Re: Whats wrong with system.diagnostics
Hey,
You are missing a using statement at the top of your code file:
Code:
using System.Diagnostics;
Either that, or change it to this:
Code:
System.Diagnostics.Process myprocess = new System.Diagnostics.Process();
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.
Gary
-
Jan 7th, 2010, 05:31 AM
#17
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
Code:
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();
}
error
Code:
Error 1 Use of unassigned local variable 'StartInfo'
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.
so if i modify the above to
Code:
SW.WriteLine("route print"); // 'the command you wish to run.....
SW.WriteLine("exit"); // 'exits command prompt window
readtext = SR.ReadToEnd();
will this help me .
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 05:35 AM
#18
Re: Whats wrong with system.diagnostics
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
-
Jan 7th, 2010, 05:46 AM
#19
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
Oh Thanks for that info.
Some clarification is needed.
Code:
SW.WriteLine("route print"); // 'the command you wish to run.....
SW.WriteLine("exit"); // 'exits command prompt window
readtext = SR.ReadToEnd();
SW.Close();
SR.Close();
i was expecting the output of route print command in the read text. But it is not. I am sure i have missed something
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 05:50 AM
#20
Re: Whats wrong with system.diagnostics
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
-
Jan 7th, 2010, 06:07 AM
#21
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
Step thriugh was working not on run, So i modified with including a sleep time
Code:
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();
This is running. A time delay was needed to write to the text file and then read from there
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 06:09 AM
#22
Re: Whats wrong with system.diagnostics
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:
Code:
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);
}
Gary
-
Jan 7th, 2010, 06:22 AM
#23
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
That was a mistake in the data displayed vs what i read . There were some additional data coming like
Code:
Microsoft Windows [Version 6.1.7600]
Copyright (c) 2009 Microsoft Corporation. All rights reserved.
which was not coming when dumping to a text file . Thanks for pointiing out the mistake
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 06:24 AM
#24
Re: Whats wrong with system.diagnostics
Hey,
That is because this command:
Code:
route print > d:\\file.txt
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.
You should be able to extract just the information that you want.
Gary
-
Jan 7th, 2010, 06:34 AM
#25
Thread Starter
Addicted Member
Re: Whats wrong with system.diagnostics
Great ,
Thanks for the info. closing this thread.
Got lot of info
Thanks Gary
If you found my reply helpful, please rate me

-
Jan 7th, 2010, 06:35 AM
#26
Re: [RESOLVED] Whats wrong with system.diagnostics
Hey,
Not a problem at all, happy to help!
Gary
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|