繁体   English   中英

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

[英]the given path format is not supported; streamwriter

下面是堆栈跟踪;

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


异常来自以下几行;

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

LogPath是通过该方法获取的;

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

我已经尝试了各种调试方式,但它总是在 StreamWriter 上抛出上述异常。 请帮助我解决这个问题。 我正在使用 4.5.2 .net 框架

文件路径

有问题的文件路径是这样的; C:\\Users\\JNyingi\\source\\repos\\EntryLogConsoleTest\\EntryLogConsoleTest\\bin\\Debug

当前时间和文件夹名称

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

string foldername = "Log"

问题是:在您的文件名中

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

将其更改为-_甚至. 例如,错误消失

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

使用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"));
        }
    }
}

所以你的路径包含无效字符。

编辑

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

暂无
暂无

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

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