Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/325.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 不支持给定的路径格式;流作者_C# - Fatal编程技术网

C# 不支持给定的路径格式;流作者

C# 不支持给定的路径格式;流作者,c#,C#,下面是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) a

下面是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


例外情况来自以下几行:

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.NETFramework

文件路径

所讨论的文件路径如下:;
C:\Users\jningi\source\repos\EntryLogConsoleTest\EntryLogConsoleTest\bin\Debug

当前时间和文件夹名称

string currentTimeFilename=DateTime.Now.ToString(“yyyy-MM-dd-HH:MM”)

string foldername=“Log”

使用您可以发现方法
emulateFileOperationChecks
(引发)的代码是:

因此,您的路径包含无效字符

编辑

如果在您的设置中,小时-分钟分隔符是冒号(请参阅您的DATEIME格式字符串),请考虑“:”不能在路径中使用,但在驱动器号之后。

< P>使用方法您可以发现代码<代码> EmulateFileIOPermissionChecks < /C> >(这引发了): 因此,您的路径包含无效字符

编辑


如果在您的设置中,小时-分钟分隔符是冒号(请参阅您的DATEIME格式字符串),请考虑“:”不能在路径中使用,但在驱动器号之后。

问题是您的文件名

string currentTimeFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm"); 
                                                                 ^
例如,将其更改为
-
-
或甚至是
,错误就会消失

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

问题是文件名中的

string currentTimeFilename = DateTime.Now.ToString("yyyy-MM-dd HH:mm"); 
                                                                 ^
例如,将其更改为
-
-
或甚至是
,错误就会消失

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

你能展示一下什么有
文件路径
?然后看看这个地址是否存在于你的系统中path@MongZhu添加了两个变量
string foldername=“Log”
这是来自调试器的值吗?如果我使用像
@“C:\foo\folderName”
这样的路径作为变量
folderName
的值,我只能复制您的异常。在将
filePath
变量发送到文件流之前,请调试代码并向我们提供该变量的结果。您能否显示
filePath
有哪些内容?然后看看这个地址是否存在于你的系统中path@MongZhu添加了两个变量
string foldername=“Log”
这是来自调试器的值吗?只有使用类似于
@“C:\foo\folderName”的路径,我才能复制您的异常
作为变量
foldername
的值,调试代码,并在将
filePath
变量发送到文件流之前给我们变量的结果。可能空格字符也是一个问题,因为URI会将其转换为
%
@Magnus否,我测试了它,即使用
%
替换
,它也能工作,但它会给出一个奇怪的结果,尽管空格字符可能也是一个问题,因为URI会将其转换为
%
@Magnus不,我测试了它,即使用
%
替换
,它也能工作,但它给出了一个奇怪的结果