简体   繁体   English

不支持给定的路径格式; 流作家

[英]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是通过该方法获取的;

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.我已经尝试了各种调试方式,但它总是在 StreamWriter 上抛出上述异常。 Kindly assist me in resolving this.请帮助我解决这个问题。 I'm using 4.5.2 .net Framework我正在使用 4.5.2 .net 框架

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:使用ILSpy,您可以发现EmulateFileIOPermissionChecks方法(引发NotSupportedException )的代码是:

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.如果在您的设置中,小时 - 分钟分隔符是一个冒号(请参阅您的日期时间格式的字符串),请考虑 ':' 不能在路径中使用,而是在驱动程序字母之后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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