繁体   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