【问题标题】:URI formats are not supported exception不支持 URI 格式异常
【发布时间】:2017-10-11 14:14:21
【问题描述】:

我是新手,我尝试打开一个文件。 这是代码:

string path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
string filenamelocation = System.IO.Path.Combine(path, "Fix_DeltaPro.exe");
System.Windows.MessageBox.Show(""+filenamelocation+"");
using (FileStream stram = File.Open(filenamelocation, FileMode.Open)) ;

但有一点错误:“不支持 URI 格式。” 请帮帮我:)

【问题讨论】:

  • 我的意思是字符串是什么样子的,你能把它记录下来(删除任何敏感的东西)吗?
  • file:\C:\Users|dimitar.grudev\documents\Visual Studio 2015' Projects\Helper\Helper\bin\Debug\filename.exe

标签: c#


【解决方案1】:

CodeBase 是在其中找到程序集的 Uri。 This can a file file://, a web location http://, or other locations.

在文件实例中,获取 Uri 的 AbsolutePath

var codeBaseUri = new Uri(Assembly.GetExecutingAssembly().CodeBase);
var path = Path.GetDirectoryName(codeBaseUri.AbsolutePath);
var filenamelocation = Path.Combine(path, "Fix_DeltaPro.exe");

MessageBox.Show(filenamelocation);
using (var stream = File.Open(filenamelocation, FileMode.Open)) ;

因为CodeBase 可以从不同的地方加载,所以使用Assembly.Location 来获取从磁盘加载程序集的位置。

var path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
var filenamelocation = Path.Combine(path, "Fix_DeltaPro.exe");

MessageBox.Show(filenamelocation);
using (var stream = File.Open(filenamelocation, FileMode.Open)) ;

另见:

【讨论】:

  • "找不到路径的一部分 'C:\\Users\\dimitar.grudev\\documents\\visual%20studio%202015\\Projects\\Helper\\Helper\\bin \\Debug\\Fix_DeltaPro.exe'。
  • @spootles:这似乎是您执行程序集所在的合理位置。你确定Fix_DeltaPro.exe实际上在同一个地方吗?
  • 是的,但我写错了。非常感谢它有效!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2015-02-15
  • 2012-09-15
  • 2017-12-12
  • 1970-01-01
  • 2016-05-08
相关资源
最近更新 更多