[英]How to make a multiple files with 1 path?
How to make a variable in a path in c#? 如何在C#中的路径中创建变量? For like creating a user register/login/stats. 对于喜欢创建用户注册/登录/统计。
string UserName = "";
string path = @"c:\File\File\" + UserName + ".text";
I know this doesn't work, maybe does anybody know how to do it else, I search around and never found a solution to get a path like this. 我知道这行不通,也许有人不知道该怎么办,我四处搜寻,却从未找到解决方案来获得这样的路径。
I hope somebody will solve it! 希望有人能解决!
您可以使用/(斜杠)代替\\(反斜杠),也可以转义反斜杠,并在其后添加另一个反斜杠:
string path = "c:\\File\\File\\"+ Username + ".text";
That way is absolutly OK for simple concating strings. 对于简单的缩进字符串,这种方式绝对可以。 There are other ways like 还有其他方式
string.Format
function string.Format
函数
or the 或者
StringBuilder
class StringBuilder
类
This is all absolutly OK but if you will be absolutly sure that you create a vaild Path use 绝对可以,但是如果您确定要创建有效的Path使用,
The only reason that doesn't work is because of the escape characters. 唯一不起作用的原因是转义字符。 Any of the following will work; 以下任何一项均可使用;
string path = "c:\\File\\File\\"+ Username + ".txt"; // escape first slash, second appears in string
string path = @"c:\File\File\"+ Username + ".text"; // take literal string, escape sequences included
string path = "c:/File/File/"+ Username + ".text"; //forward slash is not an escape
You can easily get an array of invalid file name characters 您可以轻松获取无效文件名字符的数组
char[] invalidPathChars = Path.GetInvalidPathChars();
foreach (char ch in invalidPathChars)
{
Username = Username.Replace(ch.ToString(), "");
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.