开发者

How to determine path to project folder in .Net?

开发者 https://www.devze.com 2022-12-23 05:40 出处:网络
I have a project folder called XSL which contains xsl files used for transforming xml. I use the following code to fetch an xsl file:

I have a project folder called XSL which contains xsl files used for transforming xml. I use the following code to fetch an xsl file:

string html = @"c:\temp\export.html";
XslCompiledTransform transform 开发者_开发技巧= new XslCompiledTransform();
Uri uri = new Uri(@"XSL\ToHtml.xsl", UriKind.Relative);
transform.Transform(CurrentXmlFile, html);
System.Diagnostics.Process.Start(html);

This works ok when debugging but when I deploy using clickonce and install it, I get an error - 'Could not find part of the path {my user documents path}\XSL\ToHtml.xsl'. It really needs to be looking in {installation folder}\XSL\ToHtml.xsl.

What must I do to correctly reference this path?


As already stated in DSO's answer, you should not use or implicitly depend of Environment.CurrentDirectory when you want the directory where your application executable or assemblies are located.

However I would use AppDomain.CurrentDomain.BaseDirectory instead of relying on the location of the currently executing assembly.


Relative paths are based off the current directory (Environment.CurrentDirectory), which your app has no control over. If you want to base a path off your app's install folder use this:

string file = Path.Combine(
    System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location),
    @"XSL\ToHtml.xsl");
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号