When I download an object today, I found that the object name should be retrieved from a field in the database, and this field does not guarantee that invalid characters will not be used, such \/:*? "<> |
I wrote a simple method for replacement. The Code is as follows:
/// <Summary>
/// Remove invalid characters from the file name, such \/:*? "<> |
/// </Summary>
/// <Param name = "FILENAME"> name of the file to be processed </param>
/// <Returns> processed file name </returns>
Public String replacebadcharoffilename (string filename)
{
String STR = filename;
STR = Str. Replace ("\", String. Empty );
STR = Str. Replace ("/", String. Empty );
STR = Str. Replace (":", String. Empty );
STR = Str. Replace ("*", String. Empty );
STR = Str. Replace ("? ", String. Empty );
STR = Str. Replace ("\" ", String. Empty );
STR = Str. Replace ("<", String. Empty );
STR = Str. Replace (">", String. Empty );
STR = Str. Replace ("|", String. Empty );
STR = Str. Replace ("", String. Empty); // a space is generated for the previous replacement, and the first one is replaced.
Return STR;
} The problem is that a space will be generated at the end of the process, and there is no way to replace the previous sentence.