有这样一段代码,用来编译执行CS文件。
编译执行一切顺利void doCompiler()
        {            string input = @"csc /define:DEBUG /optimize /out:Monitor.exe   " + '"' + fileName + '"';
            Process doCsc = new Process();
            doCsc.StartInfo.FileName="cmd.exe";
            doCsc.StartInfo.UseShellExecute = false;
            doCsc.StartInfo.RedirectStandardInput = true;
            doCsc.StartInfo.RedirectStandardOutput = true;
            doCsc.StartInfo.RedirectStandardError = true;
            doCsc.StartInfo.WorkingDirectory=@"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727";
            doCsc.StartInfo.CreateNoWindow = true;
            doCsc.Start();
            doCsc.StandardInput.WriteLine(input);
            doCsc.StandardInput.WriteLine("Monitor.exe");          
            doCsc.StandardInput.WriteLine("exit");
            MessageBox.Show(doCsc.StandardOutput.ReadToEnd());
            
        }被编译的CS文件中有一段是对文件进行读写的
源文件using System.IO;
using System;
using System.Collections.Generic;
using System.Text;namespace tet
{
    class Program
    {
        static int[] Counts = new int[1000];
        static string str_MonitorRst;
        static string path = "d:\\MonitorTemp.txt";
        static void SaveArr()
        {
            for (int j = 0; Counts[j] != 0; j++)
            {
                str_MonitorRst += Counts[j].ToString() + ',';
            } using (StreamWriter sw = File.CreateText(path))
            {
                sw.Write(str_MonitorRst);
                sw.Close();
            }
        }        static void Main(string[] args)
        {
            Console.WriteLine("dfd"); Counts[0]++;
            int i = 0; Counts[1]++;
            while (i < 10)
            {
                i++; Counts[2]++;
                Console.WriteLine(i.ToString()); Counts[3]++;//
            }
            SaveArr();
        }
    }
}神奇的事情出现了
我用doCompiler来编译执行,命令行也输出了。也就是i从1->10的值。
但是SaveArr();里的文件操作貌似无效,文件没有任何变化。可是我用手工用命令行来编译执行却有效的写入了文件
速度求解啊

解决方案 »

  1.   

    找到doCompiler 编译出来的exe,手工运行一下
      

  2.   

              doCsc.StandardInput.WriteLine("Monitor.exe");          
                doCsc.StandardInput.WriteLine("exit");  可能是调用的时候出了问题,Sleep 几秒钟,再 exit,看看结果
      

  3.   

    可能是Process没有修改文件的权限?
    谁懂?
      

  4.   

    试了一下,可能你路径问题。我这样做貌似对的using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Threading;
    using System.Windows.Forms;
    using System.Diagnostics;
    using System.Data;
    using System.IO;namespace CSharpConsole07
    {
        public class CsdnTest
        {
            public static void Test()
            {
                doCompiler(Path.GetDirectoryName(Environment.GetCommandLineArgs()[0]) + "\\test.cs");
            }        public static void doCompiler(string fileName)
            {            string input = @"csc /define:DEBUG /optimize /out:Monitor.exe   " + '"' + fileName + '"';
                Process doCsc = new Process();
                doCsc.StartInfo.FileName = "cmd.exe";
                doCsc.StartInfo.UseShellExecute = false;
                doCsc.StartInfo.RedirectStandardInput = true;
                doCsc.StartInfo.RedirectStandardOutput = true;
                doCsc.StartInfo.RedirectStandardError = true;
                doCsc.StartInfo.WorkingDirectory = @"C:\WINDOWS\Microsoft.NET\Framework\v2.0.50727";
                doCsc.StartInfo.CreateNoWindow = true;
                doCsc.Start();
                doCsc.StandardInput.WriteLine(input);
                doCsc.StandardInput.WriteLine("Monitor.exe");
                doCsc.StandardInput.WriteLine("exit");
                MessageBox.Show(doCsc.StandardOutput.ReadToEnd());        }        private static void TestSort01()
            {
                //2 4 5 1 3
                //Smith Jihn Mary Cherr Tomn            Student[] data = 
                { 
                    new Student("Smith ", 2), 
                    new Student("Jihn ", 4),
                    new Student("Mary", 5),
                    new Student("Cherr", 1), 
                    new Student("Tomn", 3) 
                };
                Array.Sort(data, (a, b) => a.ID - b.ID);//根据id排序
                Console.WriteLine("根据id排序");
                foreach (Student s in data) Console.WriteLine(s.Name + "-" + s.ID.ToString());
                Array.Sort(data, (a, b) => string.Compare(a.Name, b.Name, true));//根据名字排序
                Console.WriteLine("根据名字排序");
                foreach (Student s in data) Console.WriteLine(s.Name + "-" + s.ID.ToString());
            }        public class Student
            {
                public string Name;
                public int ID;
                public Student(string name, int id) { Name = name; ID = id; }
            }        //string result = Regex.Replace("0003458", @"\d+$", delegate(Match m) { return (int.Parse(m.Value) + 1).ToString().PadLeft(m.Length, '0'); });
            public Nullable<T> GetValue<T>() where T : struct
            {
                return null;
            }
        }    public class c
        {
            public string str
            {
                get
                {
                    Demo();
                    return "a";
                }
                set
                {
                    Console.WriteLine(value);
                }
            }
            public void Demo()
            {
                StackFrame frame = new StackFrame(1);//获取调用者
                if (frame.GetMethod().Name.StartsWith("get_") || frame.GetMethod().Name.StartsWith("set_"))//如果是属性,设置或读取,则是set_或get_开头的
                {
                    string property_name = frame.GetMethod().Name.Substring(frame.GetMethod().Name.IndexOf('_') + 1);//去掉set_或get_得到的就是属性名
                    this.GetType().GetProperty(property_name).SetValue(this,"test",null);//反射得到属性,调用设置。
                }
            }
        }
    }
      

  5.   

    应该不是路径的问题
    你试试用doCompiler编译一个写文件的CS文件
      

  6.   

    如果把被编译的CS文件里的FileCreatText文件操作using (StreamWriter sw = File.CreateText(path))
                {
                    sw.Write(str_MonitorRst);
                    sw.Close();
                }这部分从SaveArr()里移到Main()函数里,
    用DoCompiler就可以编译执行,并且读写文件有效了
    而放到SaveArr()里却不能读写文件。
    这时为啥
      

  7.   

    没有啊。我运行没有问题。能出来: 1,1,10,10,你看看你用DoCompiler 传进去的路径中是否带空格?
      

  8.   

    using System.IO;
    using System;
    using System.Collections.Generic;
    using System.Text;
    namespace tet
    {
        class Program
        {
            static int[] Counts = new int[1000];
            static string str_MonitorRst;
            static string path1 = "d:\\MonitorTemp1.txt";
            static void SaveArr()
            {
                for (int j = 0; Counts[j] != 0; j++)
                {
                    str_MonitorRst += Counts[j].ToString() + ',';
                }
                StreamWriter sw = File.CreateText(path1);
                sw.Write(str_MonitorRst);//这个没有写入,不成功
                sw.Close();
            }
            static void Main(string[] args)
            {
                Console.WriteLine("dfd"); Counts[0]++;
                string path2 = @"d:\MonitorTemp2.txt"; Counts[1]++;
                int i = 0; Counts[2]++;
                StreamWriter sw = File.CreateText(path2); Counts[3]++;
                sw.WriteLine("d"); Counts[4]++; //这个有效写入
                sw.Close(); Counts[5]++;
                while (i < 9)
                {
                    i++; Counts[6]++;
                    Console.WriteLine(i.ToString()); Counts[7]++;
                }            SaveArr();
            }
        }
    }
    用compiler编译这个,结果如注释!!!。。
    而用这个文件在VS里直接编译,2出文件操作均有效果why???????????
      

  9.   

    你用的是DoCompiler编译执行的????
      

  10.   

    感觉这句话有问题啊:string input = @"csc /define:DEBUG /optimize /out:Monitor.exe   " + '"' + fileName + '"';string + char + fileName + char 改为:string input = "csc /define:DEBUG /optimize /out:Monitor.exe  \"" + fileName + "\"";再看看。
      

  11.   


    直接拷你的DoCompiler。没有问题...
    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Windows.Forms;
    using System.Diagnostics;namespace DoCompiler
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }        private void button1_Click(object sender, EventArgs e)
            {
                this.openFileDialog1.ShowDialog();
                if(!string.IsNullOrEmpty(this.openFileDialog1.FileName))
                    doCompiler(this.openFileDialog1.FileName);
            }        void doCompiler(string fileName)
            {
                string input = "csc /define:DEBUG /optimize /out:Monitor.exe  \"" + fileName + "\"";
                Process doCsc = new Process();
                doCsc.StartInfo.FileName = "cmd.exe";
                doCsc.StartInfo.UseShellExecute = false;
                doCsc.StartInfo.RedirectStandardInput = true;
                doCsc.StartInfo.RedirectStandardOutput = true;
                doCsc.StartInfo.RedirectStandardError = true;
                doCsc.StartInfo.WorkingDirectory = @"E:\WINDOWS\Microsoft.NET\Framework\v2.0.50727";
                doCsc.StartInfo.CreateNoWindow = true;
                doCsc.Start();
                doCsc.StandardInput.WriteLine(input);
                doCsc.StandardInput.WriteLine("Monitor.exe");
                doCsc.StandardInput.WriteLine("exit");
                MessageBox.Show(doCsc.StandardOutput.ReadToEnd());
            }
        }
    }
      

  12.   

    再次确认,还是没问题。 Monitor.exe 也单独运行了。MonitorTemp.txt 正常
      

  13.   

    查查看是不是和操作系统是 64 位的有关,或者是因为 win7 需要使用admin来编译运行?
      

  14.   

    不是,xp。你的编译能通过?filename值不用加""?
      

  15.   

    csc参数 路径名要加 "" 才能编译的
      

  16.   

    我也用的你的代码,很正常,不是贴给你了么。就修改了下路径。结果很正常。xp+sp3
      

  17.   

    在sw.Close()之前加入sw.Flush(),以确保从磁盘缓存最终保存到了文件
      

  18.   

    csc -?
    命令里字符串有问题 是否有多余空格 最好使用@" "字符串
      

  19.   

    我的也没问题,呵呵。
    前面说的似乎有道理,执行时间的问题等等。
    建议你:
    1、把StandardError也显示出来。
    2、在StreamWriter语句附近插入输出语句,看控制台输出,以进行监视。