Click here to Skip to main content
15,867,895 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
What is the error in the following c# lines of code:

Our purpose is to open the specified file in the Text Editor without opening command prompt window:
private void btnTest_Click(object sender, EventArgs e)
        {
            // Application path and command line arguments
            string ApplicationPath  = "edit d:\\sk.txt";
            string ApplicationArguments  = " -a";

            System.Diagnostics.Process ProcessObj;
            ProcessObj = new System.Diagnostics.Process();

            // StartInfo contains the startup information of the new process
            ProcessObj.StartInfo.FileName = ApplicationPath;
            ProcessObj.StartInfo.Arguments = ApplicationArguments;

            // These two optional flags ensure that no DOS window appears
            ProcessObj.StartInfo.UseShellExecute = false;
            ProcessObj.StartInfo.CreateNoWindow = true;

            //' If this option is set the DOS window appears again :-/
                //ProcessObj.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;

            //' This ensures that you get the output from the DOS application
                ProcessObj.StartInfo.RedirectStandardOutput = true;

            //' Start the process
            ProcessObj.Start();

            //' Wait that the process exits
            ProcessObj.WaitForExit();

            //' Now read the output of the DOS application
            System.Windows.Forms.MessageBox.Show(ProcessObj.StandardOutput.ReadToEnd());
    }


Error message displayed is : "System cannot find the file specified".
Though the file "sk.txt" exist at the specified location.

Also please tell - Whether this code will fulfill our purpose of opening this file in Text Editor without opening command prompt window.

Thanks in advance.
Posted
Updated 20-May-11 5:19am
v2

Do Something like below. It should work

C#
ProcessObj.StartInfo.FileName = "Notepad";
ProcessObj.StartInfo.Arguments = "c:\\MPUsbSIn.txt"
 
Share this answer
 
Comments
sk saini 20-May-11 11:53am    
Thanks CS2011. Its working properly.
sk saini 20-May-11 12:14pm    
There's another problem has arisen to us, to run a dos command without showing command prompt window. Is it possible?
BobJanova 20-May-11 12:27pm    
Yes it is, although if you are trying to run a DOS editor, I don't understand why you'd want it not to appear, since the user won't be able to use it.

You must set UseShellExecute to false, CreateNoWindow to true and redirect stdin and stderr (not sure about stdio) to have the command window not appear.
CS2011 20-May-11 12:31pm    
If you are asking about the Edit then No it will need the DOS to be visible.But there are other commands which might work
Sergey Alexandrovich Kryukov 20-May-11 23:06pm    
Correct, a 5.
--SA
The application is 'edit', and the arguments are @'d:\sk.txt -a'. Don't put the first parameter in the application, that will (I think) cause Process.Start to fail. Also, as Fredrik stated, make sure edit is on the path.

Also, using WaitForExit on a long running process (long enough for you to edit the file) in the main UI thread is a bad idea. This will cause your primary application to freeze while you edit the file in 'edit'. You should probably use the ProcessExited event for this.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900