如何设置项目文件夹中文件的文件路径 [英] How to set file path to a file in project folder
问题描述
我正在使用C#.net 3.5并创建Windows应用程序.我想设置文件路径,该路径位于项目文件夹中,当前正在使用:
FileName = "D:\\test\\Migration\\Migration\\tools\\scanstate.exe";
但是,如果我在另一个机器上尝试该项目,则需要再次更改.
我也尝试过FileName = @"..\\tools\\scanstate.exe";
,但是它不起作用.
任何人都可以向我建议正确的方法.
谢谢.
Hi,
I am using C#.net 3.5 and creating Windows application. I want to set file path, which is in the project folder, currently I am using:
FileName = "D:\\test\\Migration\\Migration\\tools\\scanstate.exe";
But if i trie this project on another machine it needs to be changed again.
I have also tried FileName = @"..\\tools\\scanstate.exe";
but it''s not working.
Can any one suggest to me the correct way to do this.
Thank you.
推荐答案
看看 ^ ]类,它对包含文件或目录路径信息的字符串实例执行操作.
要读取/写入位于程序运行位置的文件,可以使用类似以下内容的文件:
Take a look at System.IO.Path[^] class, it performs operations on string instances that contain file or directory path information.
To read/write a file, that is located where your program runs, you can uses something like this:
string fileToRead = System.IO.Path.GetDirectoryName(Application.ExecutablePath) + @"\MyOtherFile.xml";
string allDataFromFile = System.IO.File.ReadAllText(fileToRead);
还有另一个选择是
yet another option would be
System.Windows.Forms.Application.StartupPath
祝你好运
good luck
试试这个,
Hi,
Try this,
FileName = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)) + "\\scanstate.exe";
问候,
Suresh
Regards,
Suresh
这篇关于如何设置项目文件夹中文件的文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!