I have a file called settings.xml located at:
c:\\solution1\\solution1\\data\\settings.xml
Right now, I am doing:
XDocument doc = XDocument.Load(@"c:\solution1\solution1\settings.xml");
I can't figure how to do it with a relative path.
If you mean relative to your executable, you can use
string exeLocation = System.Reflection.Assembly.GetExecutingAssembly().CodeBase
Note the frequently suggested
System.Reflection.Assembly.GetExecutingAssembly().Location
will get the path where the assembly is currently located , which can be different eg if a shadow copy is being executed.
You can use
string exeDir = System.IO.Path.GetDirectoryName(exeLocation);
to get the executable's directory.
If you want to find a file that is in a data directory under your install location, you could do
string dataFile = Path.Combine(exeDir, "data\settings.xml");
Note that under Windows Vista and later, you will not have write access by default to a directory located under your install directory.
您还可以相对于它从其开始的当前目录
System.Environment.CurrentDirectory
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.