的System.Uri未能给予正确的AbsolutePath和局部路径,如果路径中包含"#" [英] System.Uri fails to give correct AbsolutePath and LocalPath if the Path contains "#"

查看:227
本文介绍了的System.Uri未能给予正确的AbsolutePath和局部路径,如果路径中包含"#"的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有C#code,它正试图获得的localPath 的执行的程序集使用code以下行:

开放的URI =新的URI(Assembly.GetExecutingAssembly()codeBase的。).LocalPath;

这一块code进行罚款,所有的各种路径。它开始无法给​​予正确的 AbsolutePath 的localPath ,因为执行的程序集路径中包含一个#在里面。

。codeBase的

Assembly.GetExecutingAssembly()C:\ C#\ ExcelAddin1.1.0 \ GSRExcelPlugin \斌\调试\共同.DLL

但是,新的URI(Assembly.GetExecutingAssembly()codeBase的。).LocalPath给出了C:\ C,而应该给C:\ C#\ ExcelAddin1.1.0 \ GSRExcelPlugin \斌\调试\。

有没有办法,我需要处理或者是有什么错Uri类的使用方式?什么

如果这是一个.NET框架的问题,我应该怎么向微软报告此问题?

解决方案

  System.IO.FileInfo记录器=新System.IO.FileInfo(Path.Combine(Path.GetDirectoryName(新的URI (Assembly.GetExecutingAssembly()转义codeBase的。).LocalPath)的settings.config));
 

使用转义codeBase的,而不是codeBase的解决了这个问题。我力意识到,这已经被处理,直到我无意中发现了它。)

I have C# code that is trying to get the LocalPath for a executing assembly using the following line of code:

Uri uri = new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath;

This piece of code performs fine for all the variety of paths. It started to fail giving the right AbsolutePath and Localpath because the executing assembly path contained a # in it.

Assembly.GetExecutingAssembly().CodeBase gives "C:\c#\ExcelAddin1.1.0\GSRExcelPlugin\bin\Debug\Common.dll"

But new Uri(Assembly.GetExecutingAssembly().CodeBase).LocalPath gives "C:\c" while it should have given "C:\c#\ExcelAddin1.1.0\GSRExcelPlugin\bin\Debug\".

Is there something that I need to handle or is there something wrong the way Uri class is used?

If this is a .net framework issue, how should I report this issue to Microsoft?

解决方案

System.IO.FileInfo logger = new System.IO.FileInfo(Path.Combine(Path.GetDirectoryName(new Uri(Assembly.GetExecutingAssembly().EscapedCodeBase).LocalPath), "settings.config"));

Using EscapedCodeBase instead of CodeBase solves the problem. I dint realize that this was already handled until I stumbled on it.:)

这篇关于的System.Uri未能给予正确的AbsolutePath和局部路径,如果路径中包含"#"的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆