CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    Whats wrong with system.diagnostics

    i want to delete file.txt
    Code:
     System.Diagnostics.Process.Start("cmd.exe", @"/c del  d:\\file.txt");
    but this is not deleting the file. any input?
    If i helped you, Please rate me

    Visit http://www.octopusplus.com

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Whats wrong with system.diagnostics

    and if you run this command alone, does it work?

    also check the exit code
    Code:
    System.Diagnostics.Process.ExitCode
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  3. #3
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Whats wrong with system.diagnostics

    Why to do it in such a obscure way? Ins't File.Delete better?
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Whats wrong with system.diagnostics

    Quote Originally Posted by boudino View Post
    Why to do it in such a obscure way?
    for fun? to learn something new?
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

  5. #5
    Join Date
    Mar 2004
    Location
    Prague, Czech Republic, EU
    Posts
    1,701

    Re: Whats wrong with system.diagnostics

    Maybe new forum could be established: toy fixing
    • Make it run.
    • Make it right.
    • Make it fast.

    Don't hesitate to rate my post.

  6. #6
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    Re: Whats wrong with system.diagnostics

    this is my complete code
    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
    But i am getting error in the stream reader(filepath)
    Code:
    Could not find file 'd:\file.txt'..
    the File is getting genereated
    If i helped you, Please rate me

    Visit http://www.octopusplus.com

  7. #7
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    Re: Whats wrong with system.diagnostics

    Quote Originally Posted by boudino View Post
    Why to do it in such a obscure way? Ins't File.Delete better?
    i have started to do coing in c# just 4 days back. I want to get the route print command to be executed and get that to a file . So when i searched some forum, i got the reply to do with
    Code:
    System.Diagnostics.Process.Start
    To delete also , i was trying the same way. i was not aware of the other option available. Now i moved code to file.delete . thanks for the help on that .
    Is there a way i can run the command prompt from the c# other than what i have done using System.Diagnostics.Process.Start
    If i helped you, Please rate me

    Visit http://www.octopusplus.com

  8. #8
    Join Date
    Apr 2006
    Posts
    220

    Re: Whats wrong with system.diagnostics

    Quote Originally Posted by makdu View Post
    Is there a way i can run the command prompt from the c# other than what i have done using System.Diagnostics.Process.Start
    No.

  9. #9
    Join Date
    Jul 2009
    Location
    Kuwait
    Posts
    170

    Re: Whats wrong with system.diagnostics

    The file is not getting deleted
    Code:
     System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
                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
    also at first time rum ( after manually deleting the file.txt) , i am getting file not found error at
    Code:
    using (StreamReader streamReader = File.OpenText(filePath))
    hERE IS my Complete code. This code some times work proplery by reading and writing ( when there is a breakpoint), some times it is not working
    Code:
    class Program
        {
            static void deletefiles()
            {
    
                System.IO.File.Delete("d:\\madavas1.txt");// delete madavas1.txt
                System.IO.File.Delete("d:\\madavas2.txt");// delete madavas2.txt
                System.IO.File.Delete("d:\\file.txt"); //Delete the  output dump file
            }
            
            static void Main(string[] args)
            {
                string regMatch = "Active Routes:";//string to search for inside of text file. It is case sensitive.
                   string filePath = @"d:\file.txt";
                string readtext = "";
                string path = "";
                bool found = false, last = false;
    
                path = @"d:\\file.txt";
                if (!File.Exists(path))
                {
                    using (StreamWriter sw = File.CreateText(path)) // Create a file to write to madavas2.txt.
                    {
                    }
                }
                System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
                using (StreamReader streamReader = File.OpenText(filePath))
                {
                        string s = "";
                    if ((s = streamReader.ReadLine()) != null)
                    {
                        readtext = streamReader.ReadToEnd();// streamReader.ReadToEnd();
    
                    }
                }
                            
                /* delete all unwanted files */
                deletefiles();
                  
                path = @"d:\\madavas1.txt";
                if (!File.Exists(path))
                {
                    // Create a file to write to madavas1.txt.
                    using (StreamWriter sw = File.CreateText(path))
                    {
                    }
                }
                path = @"d:\\madavas2.txt";
                if (!File.Exists(path))
                {
                    // Create a file to write to madavas2.txt.
                    using (StreamWriter sw = File.CreateText(path))
                    {
                    }
                }
    
                  /* create mdavas2.txt for active route*/
    
                StringReader madvas2 = new StringReader(readtext);
                string madvas2temp = null;
    
                while ((madvas2temp = madvas2.ReadLine()) != null)
                {
                    if (Regex.IsMatch(madvas2temp, regMatch))
                    {
                        found = !found;
                    }
                    if (found)
                    {
                        if (Regex.IsMatch(madvas2temp, "======"))
                        {
                            found = false;
                            break;
                        }
                        System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\madavas2.txt", true);
                        file.WriteLine(madvas2temp);
                        file.Close();
                    }
                }
                madvas2.Close();
                /* create mdavas1.txt for persistent route*/
                 StringReader madvas1 = new StringReader(readtext);
                string madvas1temp = null;
                regMatch = "Persistent Routes:";
                while ((madvas1temp = madvas1.ReadLine()) != null)
                {
                    if (Regex.IsMatch(madvas1temp, regMatch))
                    {
                        found = !found;
                    }
                    if (found || last)
                    {
                        if (Regex.IsMatch(madvas1temp, "======"))
                        {
                            found = false;
                            break;
                        }
                        System.IO.StreamWriter file = new System.IO.StreamWriter("d:\\madavas1.txt", true);
                        file.WriteLine(madvas1temp);
                        file.Close();
                    }
                }
                madvas1.Close();
                List<string[]> sl = new List<string[]>();
                 string temp2;
                string[] delim = { " " };
                Int32 testvalue = 0;
                using (StreamReader sr = new StreamReader("d:\\madavas2.txt"))
                {
                    while ((temp2 = sr.ReadLine()) != null)
                    {
                        sl.Add(temp2.Split(delim, StringSplitOptions.RemoveEmptyEntries));
                        // testvalue=int32.parse(s1(0));
                    }
                    testvalue = Int32.Parse(sl[2][4]);
                }
                deletefiles();
            }
           }
    Is it something to do with the timer?
    Last edited by makdu; January 7th, 2010 at 02:45 AM.
    If i helped you, Please rate me

    Visit http://www.octopusplus.com

  10. #10
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: Whats wrong with system.diagnostics

    try to call
    Code:
    System.Diagnostics.Process.WaitForExit();
    right after
    Code:
    System.Diagnostics.Process.Start("cmd.exe", @"/c route print > d:\file.txt"); // Get the route print to text file
    System.Diagnostics.Process.WaitForExit()
    win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming

    remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

    private lessons are not an option so please don't ask for help in private, I won't replay

    if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

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

Featured