C#代码中的错误 [英] Error in the c# code

查看:80
本文介绍了C#代码中的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下c#代码行中的错误是什么:

我们的目的是在文本编辑器中打开指定的文件,而无需打开命令提示符窗口:

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());
    }



显示的错误消息是:系统找不到指定的文件".
尽管文件"sk.txt"位于指定位置.

还请告知-此代码是否可以满足我们在不打开命令提示符窗口的情况下在文本编辑器"中打开此文件的目的.

在此先感谢您.

解决方案

执行以下操作.它应该工作

 ProcessObj.StartInfo.FileName = " ;
ProcessObj.StartInfo.Arguments = "  


应用程序为"edit",参数为@''d:\ sk.txt -a''.不要在应用程序中放入第一个参数,否则(我认为)这将导致Process.Start失败.另外,如Fredrik所述,请确保在路径上进行编辑.

另外,在主UI线程中长时间运行的进程(足以让您编辑文件)上使用WaitForExit是一个坏主意.当您在编辑"中编辑文件时,这将导致您的主应用程序冻结.您可能应该为此使用ProcessExited事件.


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.

解决方案

Do Something like below. It should work

ProcessObj.StartInfo.FileName = "Notepad";
ProcessObj.StartInfo.Arguments = "c:\\MPUsbSIn.txt"


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.


这篇关于C#代码中的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆