Results 1 to 26 of 26

Thread: [RESOLVED] Whats wrong with system.diagnostics

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    Resolved [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

  2. #2
    Frenzied Member sciguyryan's Avatar
    Join Date
    Sep 2003
    Location
    Wales
    Posts
    1,763

    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.
    My Blog.

    Ryan Jones.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Whats wrong with system.diagnostics

    Try
    Code:
    System.IO.File.Delete("d:\\file.txt")

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    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

  5. #5
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  6. #6

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    Re: Whats wrong with system.diagnostics

    Quote Originally Posted by gep13 View Post
    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

  7. #7
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  8. #8

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    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

  9. #9
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  10. #10

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    Re: Whats wrong with system.diagnostics

    Quote Originally Posted by gep13 View Post
    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

  11. #11
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    Re: Whats wrong with system.diagnostics

    Hey,

    Why exactly did it fail? What error did you get?

    Can you post what you used?

    Gary

  12. #12
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  13. #13

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    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

  14. #14
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  15. #15

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    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

  16. #16
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  17. #17

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    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

  18. #18
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  19. #19

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    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

  20. #20
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  21. #21

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    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

  22. #22
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  23. #23

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    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

  24. #24
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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

  25. #25

    Thread Starter
    Addicted Member
    Join Date
    Jul 2009
    Posts
    140

    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

  26. #26
    PowerPoster gep13's Avatar
    Join Date
    Nov 2004
    Location
    The Granite City
    Posts
    21,963

    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
  •  



Click Here to Expand Forum to Full Width