C#在build出来的exe文件所在的根目录下或上一级文件夹里面创建一个txt文件改怎么做

或者怎么获取工程所在的路径。

第1个回答  2013-06-13
        private void Form1_Load(object sender, EventArgs e)
        {
            //debug下面exe相同路径
            string f1=Application.StartupPath + "\\test.txt";
            FileInfo fi1 = new FileInfo(f1);
            fi1.Create();

            //csproj相同路径
            string f2 = Application.StartupPath + "\\..\\..\\test.txt";
            FileInfo fi2 = new FileInfo(f2);
            fi2.Create();

            MessageBox.Show("ok");
        }

第2个回答  2013-06-13
exe所在目录可以用 System.Windows.Forms.Application.StartupPath 取到,上一级当然就是
System.Windows.Forms.Application.StartupPath+@"\..\"
根目录你取它的前三个字符就行了。本回答被提问者采纳
第3个回答  2013-06-13
我记得有一个方法是可以获取可执行文件的路径。。。
相似回答