我想通过C#编写应用程序,实现在windows的dos(Command)环境中执行一条type d:\1.txt的命令。

解决方案 »

  1.   


     关键要看你用不用管道做重定向在winform中显示结果了  参考代码:  private Process p;
      p = new Process();
      p.StartInfo.FileName = "cmd.exe";
      p.StartInfo.UseShellExecute = false; //不启动窗口
      p.StartInfo.RedirectStandardInput = true; //重定向输入
      p.StartInfo.RedirectStandardOutput = true; //重定向输出
      p.StartInfo.CreateNoWindow = true; //不显示Console
      p.Start();  //////////////////
      p.StandardInput.WriteLine("type xxx.txt");// 向cmd.exe输入command  /////////////////  string s = p.StandardOutput.ReadToEnd();// 得到cmd.exe的输出
      s = s.Replace("\r","").Replace("\n","\r\n");  ////////////////  那个 s 就可以在 winform中显示了 #####################################
      注意用多线程 否则UI线程会失去响应的
     #####################################
      

  2.   

    更正一下上面,p.StandardInput.WriteLine("type xxx.txt");后面必须还有一句
    p.StandardInput.WriteLine("exit");
    否则永远执行不完
      

  3.   

    如果我只想得到文本的内容,而不希望得到前面的dos提示和后边的路径,两位有没有办法呢?另外Vs.net2003的命令提示窗口需要在运行框中输入什么命令启动?
      

  4.   

    [C#] 
    using System;
    using System.Diagnostics;
    using System.ComponentModel;namespace MyProcessSample
    {
        /// <summary>
        /// Shell for the sample.
        /// </summary>
        public class MyProcess
        {
            // These are the Win32 error code for file not found or access denied.
            const int ERROR_FILE_NOT_FOUND =2;
            const int ERROR_ACCESS_DENIED = 5;        /// <summary>
            /// Prints a file with a .doc extension.
            /// </summary>
            public void PrintDoc()
            {
                Process myProcess = new Process();
                
                try
                {
                    // Get the path that stores user documents.
                    string myDocumentsPath = 
                        Environment.GetFolderPath(Environment.SpecialFolder.Personal);                myProcess.StartInfo.FileName = myDocumentsPath + "\\MyFile.doc"; 
                    myProcess.StartInfo.Verb = "Print";
                    myProcess.StartInfo.CreateNoWindow = true;
                    myProcess.Start();
                }
                catch (Win32Exception e)
                {
                    if(e.NativeErrorCode == ERROR_FILE_NOT_FOUND)
                    {
                        Console.WriteLine(e.Message + ". Check the path.");
                    }                 else if (e.NativeErrorCode == ERROR_ACCESS_DENIED)
                    {
                        // Note that if your word processor might generate exceptions
                        // such as this, which are handled first.
                        Console.WriteLine(e.Message + 
                            ". You do not have permission to print this file.");
                    }
                }
            }
            public static void Main()
            {
                MyProcess myProcess = new MyProcess();
                myProcess.PrintDoc();
            }
        }
    }
      

  5.   

    http://blog.csdn.net/zhzuo/archive/2004/03/21/22024.aspx