2017-10-11 92 views
0

我是新手,我尝试打开一个文件。 这里是代码:URI格式不支持异常

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格式不支持” 请帮助我:)

+0

我的意思是什么字符串的样子,你可以登录它(删除任何敏感的)? – msanford

+0

file:\ C:\ Users | dimitar.grudev \ documents \ Visual Studio 2015'Projects \ Helper \ Helper \ bin \ Debug \ filename.exe – spootles

回答

3

CodeBase是一个开放的,其中装配被发现。 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)) ; 

参见:

+0

“找不到路径的一部分'C:\\ Users \\ dimitar.grudev \\ documents \\ visual%20studio%202015 \\ Projects \\ Helper \\ Helper \\ bin \\ Debug \\ Fix_DeltaPro.exe' – spootles

+0

@spootles:对于执行程序集的位置,这似乎是一个合理的位置。你确定'Fix_DeltaPro.exe'实际上是在同一个地方吗? – Chris

+0

是的,但我写错了。非常感谢你! – spootles