4

FileName contains e.g. 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?

klashar
  • 2,519
  • 2
  • 28
  • 38
Paule Paul
  • 387
  • 3
  • 11
  • 1
    Filenames can't contain slash characters. It is filesystem limitation. – Andrey Korneyev Feb 16 '17 at 13:59
  • instead of using "/" for further processing, can you use other char to further process? there is a discussion on the [illegal file name for different OS](http://stackoverflow.com/questions/1976007/what-characters-are-forbidden-in-windows-and-linux-directory-names) – Turbot Feb 16 '17 at 14:00
  • can you explain what you are trying to do? why can't you use another symbol? why do you need to throw a string with some split marker around? – Dennis Christian Feb 16 '17 at 14:06
  • i will replace the illegal characters. I didn't want to waste unnecessary computing power, But as I see it the only way – Paule Paul Feb 16 '17 at 14:18

3 Answers3

5

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

You need to escape it somehow (i.e. 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.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
  • That is really too bad :/ Thanks for your prompt reply. i will replace the illegal characters. :) I didn't want to waste unnecessary computing power, But as I see it the only way – Paule Paul Feb 16 '17 at 14:16
1

There are rules for names and folders defined by Microsoft that mean you are not allowed to do this.

Bathsheba
  • 231,907
  • 34
  • 361
  • 483
0

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.

Joel Harkes
  • 10,975
  • 3
  • 46
  • 65