How do you correctly escape a document name in .NET?

China☆狼群 提交于 2019-12-05 02:07:59

Have a look at the Uri.EscapeDataString Method:

Uri.EscapeDataString("Hello There.docx")  // "Hello%20There.docx"

Uri.EscapeDataString("Hello#There.docx")  // "Hello%23There.docx"

I would approach it a different way: Do not use the document name as key in your look-up - use a Guid or some other id parameter that you can map to the document name on disk in your database. Not only would that guarantee uniqueness but you also would not have this problem of escaping in the first place.

You can use @ character to escape strings. See the below pieces of code.

string str = @"\n\n\n\n";
 Console.WriteLine(str);

Output: \n\n\n\n

string str1 = @"\df\%%^\^\)\t%%";
Console.WriteLine(str1);

Output: \df\%%^\^)\t%%

This kind of formatting is very useful for pathnames and for creating regexes.

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!