简体   繁体   中英

the given path format is not supported; streamwriter

Below are the Stacktrace;

System.NotSupportedException
  HResult=0x80131515
  Message=The given path's format is not supported.
  Source=mscorlib
  StackTrace:
   at System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boolean useLongPath, Boolean checkHost)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   at EntryLog.Handlers.StreamEntryLogs.StreamWritter(String log, String foldername) in C:\Users\JNyingi\source\repos\EntryLog\EntryLog\Handlers\StreamEntryLogs.cs:line 31
   at EntryLog.EntryLog.LogWarning(String Warning) in C:\Users\JNyingi\source\repos\EntryLog\EntryLog\EntryLog.cs:line 55
   at EntryLogConsoleTest.Program.Main(String[] args) in C:\Users\JNyingi\source\repos\EntryLogConsoleTest\EntryLogConsoleTest\Program.cs:line 21

  This exception was originally thrown at this call stack:
    System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(string)
    System.IO.FileStream.Init(string, System.IO.FileMode, System.IO.FileAccess, int, bool, System.IO.FileShare, int, System.IO.FileOptions, Microsoft.Win32.Win32Native.SECURITY_ATTRIBUTES, string, bool, bool, bool)
    System.IO.FileStream.FileStream(string, System.IO.FileMode, System.IO.FileAccess)
    EntryLog.Handlers.StreamEntryLogs.StreamWritter(string, string) in StreamEntryLogs.cs
    EntryLog.EntryLog.LogWarning(string) in EntryLog.cs
    EntryLogConsoleTest.Program.Main(string[]) in Program.cs


The exception is coming about from the following lines;

string filePath = System.IO.Path.Combine(EntryLog.LogPath.AbsolutePath, currentTimeFilename + " - " + $"{foldername}.log");


var fileStreamer = new FileStream(filePath, FileMode.OpenOrCreate, FileAccess.Write);
var streamWriter = new StreamWriter(fileStreamer);

The LogPath is obtained by this method;

LogPath = new Uri(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location)); 

I have tried all manner of debugging but it always throws the above exception at StreamWriter. Kindly assist me in resolving this. I'm using 4.5.2 .net Framework

FILE PATH

The file path in question is this; C:\\Users\\JNyingi\\source\\repos\\EntryLogConsoleTest\\EntryLogConsoleTest\\bin\\Debug

CURRENT TIME AND FOLDER NAME

string currentTimeFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm");

string foldername = "Log"

the problem is the : in your filename

string currentTimeFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm"); 
                                                                 ^

Change it to - or _ or even a . for example and the error disappears

string currentTimeFilename = DateTime.Now.ToString("yyyy-MM-dd HH_mm"); 

Using ILSpy you can find that the code of the method EmulateFileIOPermissionChecks (which raises the NotSupportedException ) is:

internal static void EmulateFileIOPermissionChecks(string fullPath)
{
    if (AppContextSwitches.UseLegacyPathHandling || !PathInternal.IsDevice(fullPath))
    {
        if (PathInternal.HasWildCardCharacters(fullPath))
        {
            throw new ArgumentException(Environment.GetResourceString("Argument_InvalidPathChars"));
        }
        if (PathInternal.HasInvalidVolumeSeparator(fullPath))
        {
            throw new NotSupportedException(Environment.GetResourceString("Argument_PathFormatNotSupported"));
        }
    }
}

So your path contains invalid chars.

EDIT

If in your settings hours - minutes separator is a colon (see your datetime formatted string), please consider that ':' cannot be used in a path, but after driver letter.

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