简体   繁体   中英

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

I have this code that should assign a string path value to the "uripath" string variable:

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);
}

...but it crashes when trying to execute the " _fileStream = File.OpenWrite(uriPath); " line.

Why, and what do I need to do to rectify this revoltin' development?

The value of "uriPath" when it crashes is:

file:\\C:\\Projects\\ReportScheduler\\ReportScheduler\\bin\\Debug\\AppLogMsgs.txt

If your intent is to know the location of the loaded file that contains the manifest then you could change your method to

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

See Location Property

AssemblyName.CodeBase property returns the location of the assembly as a URL and as the assembly is a local file the string begins with file:\\

Just need to remove this part from uriPath and should work as expected:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM