![](/img/trans.png)
[英]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.