URI格式不受支持异常

时间:2017-10-11 14:14:21

标签: c#

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

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格式。” 请帮帮我:)。

1 个答案:

答案 0 :(得分:3)

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)) ;

另见:

相关问题