2017-10-11 93 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