在cmd下,
ping 192.168.0.111 > c:\a.txt
可以把文件写入文件中。但,点击操作系统的‘开始-》运行’,直接输入这个命令则无效。
run函数的效果与‘开始-》运行’一样无效。
制作bat,run bat文件当然可以解决,请问有更好的办法嘛?

解决方案 »

  1.   

    就在cmd里敲不就行了吗?不知道你觉得怎样的方法才算是好方法?
      

  2.   

            [DllImport("shell32.dll", EntryPoint = "ShellExecute")]
            public static extern int ShellExecute(
                int hwnd,
                string lpOperation,
                string lpFile,
                string lpParameters,
                string lpDirectory,
                int nShowCmd
            );        private void button1_Click(object sender, EventArgs e)
            {
                ShellExecute(0, "open", "cmd.exe", "/C ping 192.168.1.1 >>C:\\11.TXT", null, 1);
            }
      

  3.   

          [DllImport("kernel32.dll", EntryPoint = "WinExec")]
            public static extern int WinExec(
                string lpCmdLine,
                int nCmdShow
            );        private void button1_Click(object sender, EventArgs e)
            {            WinExec("cmd.exe /C ping 192.168.1.1 >>C:\\22.TXT", 0);
            }
      

  4.   

           private void button1_Click(object sender, EventArgs e)
            {
                System.Diagnostics.Process.Start("cmd.exe", "/c ping 192.168.1.1 >>c:\\33.txt");
            }
      

  5.   

    开始-》运行,相当于是你进行硬盘搜索相应的指令,搜索到后来执行,那当然是没有ping 192.168.0.111 > c:\a.txt 这个命令了啊
    而你在cmd.exe是下操作的是一个32位的命令行程序,就像是DOS命令一样
      

  6.   

                Process p = new Process();
                p.StartInfo.FileName = "cmd.exe";
                p.StartInfo.UseShellExecute = false;//不用Shell启动
                p.StartInfo.RedirectStandardInput = true;//重定向输入
                p.StartInfo.RedirectStandardOutput = true;//重定向输出
                p.StartInfo.CreateNoWindow = true;//不显示窗口
                p.Start();
                p.StandardInput.WriteLine("ping 192.168.0.111");
                p.StandardInput.WriteLine("exit");
                string str = p.StandardOutput.ReadToEnd();
                File.AppendAllText("c:\\a.txt", str);