怎样在代码中设置进入某个路径?

解决方案 »

  1.   

    ing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using Microsoft.Win32;
    using System.Runtime.InteropServices;
    using System.Reflection;
    //调用API函数需要的引用,来加载非托管类user32.dll namespace WindowsApplication3
    {
       
          public partial class Form1 : Form
      {
         
        System.IO.StreamWriter sw;  // 定义输出流 sw 作为Shell的标准输入,即命令 
        System.IO.StreamReader sr;  // 定义输出流 sr 作为Shell的标准输出,即正常结果
        System.IO.StreamReader err; // 定义输出流 err 作为Shell的错误输出,即出错结果
          
        System.Diagnostics.Process p = new System.Diagnostics.Process();
          
         System.Diagnostics.ProcessStartInfo psI = new System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("aa"));      string Lujing;
          
          
        public Form1()
        {
           // string Lujing = Environment.GetFolderPath(Environment.SpecialFolder.System);//就能获得%system%了 
           
          Lujing=  Path.GetDirectoryName(Assembly.GetAssembly(typeof(int)).Location);//获得framework 的安装路径      //  Environment.SetEnvironmentVariable("aa", @"%comspec% /k ""C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat"" x86");
          InitializeComponent();
        }
          
              string path;     
                private void Form1_Load(object sender, EventArgs e)
          {
             
          }
          private void bianyi()
          {
              p.StartInfo.UseShellExecute =false  ;
              p.StartInfo.RedirectStandardInput = true;
              p.StartInfo.RedirectStandardOutput = true      ;
              p.StartInfo.RedirectStandardError = true;
              p.StartInfo.CreateNoWindow = true  ;
              p.StartInfo.FileName = "cmd.exe";
             
              Cursor = Cursors.WaitCursor;
                       /*
              psI.UseShellExecute = false;
             psI.RedirectStandardInput = true;
             psI.RedirectStandardOutput = true;
            psI.RedirectStandardError = true;
             psI.CreateNoWindow = true;
             p.StartInfo = psI ;
               * */
              try
              {
                  p.Start();
              }
              catch (Exception sf)
              {
                  MessageBox.Show(sf.Message);
              }
              sw = p.StandardInput;
          sr = p.StandardOutput;
              err = p.StandardError;
              sw.AutoFlush = false; //true
            
              sw.WriteLine("cd "+Lujing );
             
              sw.Write ("csc ");
              sw.WriteLine(path);          int i = path.LastIndexOf("\\");
              int j = path.LastIndexOf(".");
              string filename = path.Substring(i + 1, (j - i - 1));
              sw.WriteLine(filename);
                sw.Close();          string str;
           str = "输出结果为:\n" + sr.ReadToEnd();
              MessageBox.Show(str);
              // tbResult.Text += "\n错误信息:\n" + err.ReadToEnd();          Cursor = Cursors.Default;      }      private void button1_Click(object sender, EventArgs e)
          {
              
              OpenFileDialog op = new OpenFileDialog();
              op.ShowDialog();
              path  = op.FileName;
              textBox1.Text = path;
              if (path != "")
              {
                  bianyi();
              }
              else
                  return;
             // MessageBox.Show(path);
          }
      }为什么这句 sw.WriteLine("cd "+Lujing );
    进入不了该路径应该怎样改?
      

  2.   

    ing System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using Microsoft.Win32;
    using System.Runtime.InteropServices;
    using System.Reflection;
    //调用API函数需要的引用,来加载非托管类user32.dll namespace WindowsApplication3
    {
       
          public partial class Form1 : Form
      {
         
        System.IO.StreamWriter sw;  // 定义输出流 sw 作为Shell的标准输入,即命令 
        System.IO.StreamReader sr;  // 定义输出流 sr 作为Shell的标准输出,即正常结果
        System.IO.StreamReader err; // 定义输出流 err 作为Shell的错误输出,即出错结果
          
        System.Diagnostics.Process p = new System.Diagnostics.Process();
          
         System.Diagnostics.ProcessStartInfo psI = new System.Diagnostics.ProcessStartInfo(System.Environment.GetEnvironmentVariable("aa"));      string Lujing;
          
          
        public Form1()
        {
           // string Lujing = Environment.GetFolderPath(Environment.SpecialFolder.System);//就能获得%system%了 
           
          Lujing=  Path.GetDirectoryName(Assembly.GetAssembly(typeof(int)).Location);//获得framework 的安装路径      //  Environment.SetEnvironmentVariable("aa", @"%comspec% /k ""C:\Program Files\Microsoft Visual Studio 8\VC\vcvarsall.bat"" x86");
          InitializeComponent();
        }
          
              string path;     
                private void Form1_Load(object sender, EventArgs e)
          {
             
          }
          private void bianyi()
          {
              p.StartInfo.UseShellExecute =false  ;
              p.StartInfo.RedirectStandardInput = true;
              p.StartInfo.RedirectStandardOutput = true      ;
              p.StartInfo.RedirectStandardError = true;
              p.StartInfo.CreateNoWindow = true  ;
              p.StartInfo.FileName = "cmd.exe";
             
              Cursor = Cursors.WaitCursor;
                       /*
              psI.UseShellExecute = false;
             psI.RedirectStandardInput = true;
             psI.RedirectStandardOutput = true;
            psI.RedirectStandardError = true;
             psI.CreateNoWindow = true;
             p.StartInfo = psI ;
               * */
              try
              {
                  p.Start();
              }
              catch (Exception sf)
              {
                  MessageBox.Show(sf.Message);
              }
              sw = p.StandardInput;
          sr = p.StandardOutput;
              err = p.StandardError;
              sw.AutoFlush = false; //true
            
              sw.WriteLine("cd "+Lujing );
             
              sw.Write ("csc ");
              sw.WriteLine(path);          int i = path.LastIndexOf("\\");
              int j = path.LastIndexOf(".");
              string filename = path.Substring(i + 1, (j - i - 1));
              sw.WriteLine(filename);
                sw.Close();          string str;
           str = "输出结果为:\n" + sr.ReadToEnd();
              MessageBox.Show(str);
              // tbResult.Text += "\n错误信息:\n" + err.ReadToEnd();          Cursor = Cursors.Default;      }      private void button1_Click(object sender, EventArgs e)
          {
              
              OpenFileDialog op = new OpenFileDialog();
              op.ShowDialog();
              path  = op.FileName;
              textBox1.Text = path;
              if (path != "")
              {
                  bianyi();
              }
              else
                  return;
             // MessageBox.Show(path);
          }
      }为什么这句     sw.WriteLine("cd "+Lujing );
    进入不了该路径我是调用framework安装目录下的CSC命令,DOS命令没错为什么进不了。
      

  3.   


        string input = "c:\\temp\\a.cs";
        string exePath = "%SystemRoot%\\Microsoft.Net\\Framework\\v2.0.50727\\csc";
        string outputPath = System.IO.Path.ChangeExtension(input, ".exe");    exePath = System.Environment.ExpandEnvironmentVariables(exePath);
        string param = string.Format("/C {0} /OUT:\"{1}\" {2}", exePath, outputPath, input);    System.Diagnostics.Process proc = System.Diagnostics.Process.Start("cmd", param);
        proc.WaitForExit();http://topic.csdn.net/u/20080519/13/833c6d0c-a25a-4ab5-8667-de6ab2d0b4eb.html