下面是堆栈跟踪;
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:UsersJNyingisourcereposEntryLogEntryLogHandlersStreamEntryLogs.cs:line 31
at EntryLog.EntryLog.LogWarning(String Warning) in C:UsersJNyingisourcereposEntryLogEntryLogEntryLog.cs:line 55
at EntryLogConsoleTest.Program.Main(String[] args) in C:UsersJNyingisourcereposEntryLogConsoleTestEntryLogConsoleTestProgram.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 = new Uri(Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location));
我已经尝试了各种调试方式,但它总是在 StreamWriter 上抛出上述异常。请协助我解决这个问题。我正在使用 4.5.2 .net 框架
文件路径
有问题的文件路径是这样的;C:UsersJNyingisourcereposEntryLogConsoleTestEntryLogConsoleTestbinDebug
当前时间和文件夹名称
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"));
}
}
}
因此,您的路径包含无效字符。
编辑
如果在设置中小时 - 分钟分隔符是冒号(请参阅日期时间格式的字符串(,请考虑":"不能在路径中使用,而是在驱动程序字母之后使用。