检查控制字符

Imr*_*zad 1 .net c# unicode ravendb

我想检查一个字符串以确保它不包含任何控制字符。我已经想出了这个解决方案。您是否同意它足以达到预期目的?有什么明显的我错过了吗?

\n\n
        string input = "2cancer\xef\xbc\x87.pdf";\n\n        char[] chars = input.ToCharArray();\n\n        foreach (var c in chars)\n        {\n            if(c == 127 || (c < \' \' && c != \'\\t\'))\n            {\n                throw new Exception("Control character detected");\n            }\n        }     \n
Run Code Online (Sandbox Code Playgroud)\n\n

其背景是将文件上传到 RavenDB 时出现问题,它抱怨文件名包含无效的控制字符,即 \xef\xbc\x87,所以我希望检测到无效的控制字符,然后将其删除或抛出警告用户的例外,尽管我可能会做前者。

\n\n

异常的堆栈跟踪是:

\n\n
    [ArgumentException: Specified value has invalid Control characters.\n    Parameter name: value]\n       System.Net.WebHeaderCollection.CheckBadChars(String name, Boolean isHeaderValue) +8426470\n       System.Net.WebHeaderCollection.Set(String name, String value) +86\n       Raven.Client.Connection.HttpJsonRequest.WriteMetadata(RavenJObject metadata) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\HttpJsonRequest.cs:573\n       Raven.Client.Connection.HttpJsonRequest..ctor(CreateHttpJsonRequestParams requestParams, HttpJsonRequestFactory factory) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\HttpJsonRequest.cs:91\n       Raven.Client.Connection.HttpJsonRequestFactory.CreateHttpJsonRequest(CreateHttpJsonRequestParams createHttpJsonRequestParams) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\HttpJsonRequestFactory.cs:57\n       Raven.Client.Connection.ServerClient.DirectPutAttachment(String key, RavenJObject metadata, Nullable`1 etag, Stream data, String operationUrl) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\ServerClient.cs:490\n       Raven.Client.Connection.<>c__DisplayClass19.<PutAttachment>b__18(String operationUrl) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\ServerClient.cs:441\n       Raven.Client.Connection.<>c__DisplayClassa.<ExecuteWithReplication>b__9(String operationUrl) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\ServerClient.cs:167\n       Raven.Client.Connection.ReplicationInformer.TryOperation(Func`2 operation, String operationUrl, Boolean avoidThrowing, T& result) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\ReplicationInformer.cs:564\n       Raven.Client.Connection.ReplicationInformer.ExecuteWithReplication(String method, String primaryUrl, Int32 currentRequest, Int32 currentReadStripingBase, Func`2 operation) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\ReplicationInformer.cs:523\n       Raven.Client.Connection.ServerClient.ExecuteWithReplication(String method, Func`2 operation) in c:\\Builds\\RavenDB-Stable\\Raven.Client.Lightweight\\Connection\\ServerClient.cs:174\n
Run Code Online (Sandbox Code Playgroud)\n\n

我知道这是我正在使用的 RavenDB 版本中的一个错误,但是目前我无法升级数据库。

\n

vcs*_*nes 5

用于Char.IsControl检查字符是否为控制字符。

  • 这可以简化您对“input.Any(char.IsControl)”的检查 (5认同)