Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.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 txt文件StreamWriter的自定义名称_C#_Streamwriter - Fatal编程技术网

C# c txt文件StreamWriter的自定义名称

C# c txt文件StreamWriter的自定义名称,c#,streamwriter,C#,Streamwriter,我需要一个名为username\u currentdate.txt的文本文件 string username = Console.ReadLine(); string place = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments); string date = DateTime.Now.ToString("h:mm:ss"); StreamWriter FajlW = new StreamWriter(Path.C

我需要一个名为username\u currentdate.txt的文本文件

string username = Console.ReadLine();
string place = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);
string date = DateTime.Now.ToString("h:mm:ss");
StreamWriter FajlW = new StreamWriter(Path.Combine(place,username + "_" + date + ".txt"));
我在VisualStudio中没有收到任何错误,但是当我打开它时,我得到了很多错误。

我假设文件名中不允许使用冒号,这至少适用于Windows和UNIX系统。因此,请尝试使用另一个允许的分隔符:

string date = DateTime.Now.ToString("h_mm_ss");

文件名不能包含以下任何字符:\/:*?<>|


你的问题是日期格式。声明:


您需要选择一种没有:的格式。

这是因为,您已将要格式化的日期指定为DateTime。现在。toString:mm:ss。请使用任何其他字符代替“:”,因为文件夹路径中不支持“:”。

能否以英文文本而非图像的形式向我们提供例外情况?:文件名的字符无效如果此答案对您有帮助,请将其标记为答案
Use any character in the current code page for a name, including Unicode characters and characters in the extended character set (128–255), except for the following:

    The following reserved characters:
        < (less than)
        > (greater than)
        : (colon)
        " (double quote)
        / (forward slash)
        \ (backslash)
        | (vertical bar or pipe)
        ? (question mark)
        * (asterisk)

    Integer value zero, sometimes referred to as the ASCII NUL character.

    Characters whose integer representations are in the range from 1 through 31, except for alternate data streams where these characters are allowed. For more information about file streams, see File Streams.

    Any other character that the target file system does not allow.