【发布时间】:2013-01-06 20:40:43
【问题描述】:
我在一个静态类中有一个公共属性,我想用它来验证用户输入的非法文件和路径名。
目前我有这个:
private static string invalidFileNames = new string(Path.GetInvalidFileNameChars());
private static string invalidPathNames = new string(Path.GetInvalidPathChars());
private static string invalidUserInput = (invalidFileNames + invalidPathNames);
public static string InvalidUserInput
{
get { return invalidUserInput; }
}
基于 Microsoft 的文档 here 我期待回复 "<>|"<>| 但我得到的只是第一个 "<>|。
谁能解释一下这里发生了什么?如何确保返回两个字符串?
【问题讨论】:
-
就诊断问题而言,您尝试了哪些方法?您是否考虑过分别返回两个组件字符串以确保它们包含您认为它们应该包含的内容?
-
将
InvalidUserInput分配给一个变量似乎为我返回了正确的值。 -
@David - 如果我注释掉两个 char 数组并用“string a”和“string b”替换它们,我会按预期返回字符串 a 和字符串 b(stringastringb),但是,如果我使用private static string invalidPathNames = "string b" 我得到的只是 "|
标签: c# string properties path char