[英]Why am I obtaining “URI formats are not supported” exception trying to write into a log file using C#?
[英]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.