简体   繁体   中英

C# Save file with “/” in name

FileName contains eg Legend/Dery//Times

File.WriteAllBytes("/Pictures" + FileName, buffer);

I can´t save the file because the "/" considered as path, I also can´t remove the "/", because I need it for further processing. Is there any way of saving such file?

You're out of luck. A forward slash can't be part of a file name.

You need to escape it somehow (ie change the name but provide a way of changing it back), but there isn't really a conventional way of doing that.

I've seen % been used for this purpose, with %% used to denote a single % , and something like %f for a forward slash, %b for a backslash, etc.

Microsoft定义了一些名称和文件夹规则,这意味着您不能这样做。

Instead of escaping in i suggest normalizing your input both when you save a file and when you try to access a file:

//replace all illegal characters with regex (with a dash):
new Regex(@"[<>:""/\\|?*]").Replace("Inpu|t","-")

//Or just replace all non alpha numeric characters (with a dash): 
new Regex(@"[^a-zA-Z0-9\-]").Replace("Inpu|t","-")

this way you will always have clean file and folder names and don't have to worry about illegal names.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM