为什么我得到 "URI formats are not supported" 这个代码?
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
属性 returns 程序集的位置作为 URL 并且由于程序集是本地文件,字符串以 file 开头:\
只需要从 uriPath
中删除这部分,应该会按预期工作:
string uriPath = GetExecutionFolder() + "\AppLogMsgs.txt";
uriPath = uriPath.Replace("file:\", string.Empty);
_fileStream = File.OpenWrite(uriPath);
我有这段代码应该将字符串路径值分配给 "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
属性 returns 程序集的位置作为 URL 并且由于程序集是本地文件,字符串以 file 开头:\
只需要从 uriPath
中删除这部分,应该会按预期工作:
string uriPath = GetExecutionFolder() + "\AppLogMsgs.txt";
uriPath = uriPath.Replace("file:\", string.Empty);
_fileStream = File.OpenWrite(uriPath);