文件名有两个反斜杠C# [英] File name has two backslashes C#

查看:401
本文介绍了文件名有两个反斜杠C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对此可能有一个简单的答案,但是当我在我的fileName中添加 DateTime.Now.ToString()时,我每增加一个\就额外增加一个\因此, C:\Temp 变为 C:\\Temp ,导致文件无法保存。

There is probably an easy answer for this, but when I added DateTime.Now.ToString() to my fileName it adds an extra \ for every \ I have so C:\Temp becomes C:\\Temp which causes the file not to save.

这是有问题的代码

String fileName = @"C:\Temp\data_" + DateTime.Now.ToString() + ".txt";

例如,输出可能是 C:\\Temp\ \data_12 / 04/2012 20:08:40.txt

应为 C:\Temp \data_12 / 04/2012 20:08:40.txt

推荐答案

不是,那个字符串 really 中包含单个反斜杠。将其打印到控制台上,您会看到它。

Nope, that string really has single backslashes in. Print it out to the console and you'll see that.

如果您在 debugger ,您会看到反斜杠转义了-但是字符串本身只有一个反斜杠。这咬了很多人:(

If you look at it in the debugger, you'll see the backslashes escaped - but the string itself has single backslashes. This bites lots of people :(

这篇关于文件名有两个反斜杠C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆