簡體   English   中英

為什么我得到的代碼“不支持URI格式”?

[英]Why am I getting, “URI formats are not supported” with this code?

我有此代碼應為“ uripath”字符串變量分配一個字符串路徑值:

private readonly FileStream _fileStream;

. . .

string uriPath = GetExecutionFolder() + "\\AppLogMsgs.txt";
_fileStream = File.OpenWrite(uriPath); 

. . .

private string GetExecutionFolder()
{
    return Path.GetDirectoryName       
(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase);
}

...但是當嘗試執行“ _fileStream = File.OpenWrite(uriPath); ”行時,它崩潰了。

為什么,我需要做什么來糾正這種反抗的發展?

崩潰時的“ uriPath”值為:

文件:\\ C:\\ Projects \\ ReportScheduler \\ ReportScheduler \\ bin \\ Debug \\ AppLogMsgs.txt

如果您想知道包含清單的已加載文件的位置則可以將方法更改為

private string GetExecutionFolder()
{
    return Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location);
}

查看位置屬性

AssemblyName.CodeBase屬性以URL形式返回程序集的位置,並且由於程序集是本地文件,因此字符串以file:\\開頭

只需要將其從uriPath刪除, uriPath按預期工作:

string uriPath = GetExecutionFolder() + "\\AppLogMsgs.txt";
uriPath = uriPath.Replace("file:\\", string.Empty);
_fileStream = File.OpenWrite(uriPath); 

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM