-1

I'm making a small program that will auto generate all of the files that I need to make a website like index.html and style.css so when I make a new site it will be easy to start coding. I want to make my program not accept any illegal file/folder names and characters into the text box. I just dont want the <>:/|\?* but I also want the names like con and nul to not work.

I am using c# winforms with Visual Studio 2019 and dotnet 3

If anyone has an answer please tell me.

Max McCarthy
  • 50
  • 1
  • 4
  • 9
  • Does this answer your question? [How to remove illegal characters from path and filenames?](https://stackoverflow.com/questions/146134/how-to-remove-illegal-characters-from-path-and-filenames). You have to check yourself to remove `con` and `nul`, because they could be part of a legitimate filename (like `consolidations.txt` or `nullvaluesfromtable.txt`). – Ken White Dec 03 '20 at 00:52
  • `that will auto generate all of the files` If you are auto generating, call them whatever you want. – mjwills Dec 03 '20 at 01:00

2 Answers2

1

The characters that are not allowed in a file name are available from the Path.GetInvalidFileNameChars() method (but note the "Remarks" section in the documentation). For the reserved file names, I'm not aware of a method to get them, so you would have to compare against all of them explicitely:

bool IsValidFileName(string name)
{
    // empty names are not allowed
    if (string.IsNullOrEmpty(name))
        return false;
    
    // check for characters that are not allowed in a file name
    if (Path.GetInvalidFileNameChars().Any(c => name.Contains(c))
        return false;
    
    // check for reserved names
    if (name.Equals(".", StringComparison.OrdinalIgnoreCase)
        || name.Equals("..", StringComparison.OrdinalIgnoreCase)
        || name.Equals("CON", StringComparison.OrdinalIgnoreCase)
        || name.Equals("PRN", StringComparison.OrdinalIgnoreCase)
        || name.Equals("AUX", StringComparison.OrdinalIgnoreCase)
        || name.Equals("NUL", StringComparison.OrdinalIgnoreCase)
        || name.Equals("COM1", StringComparison.OrdinalIgnoreCase)
        || name.Equals("COM2", StringComparison.OrdinalIgnoreCase)
        || name.Equals("COM3", StringComparison.OrdinalIgnoreCase)
        || name.Equals("COM4", StringComparison.OrdinalIgnoreCase)
        || name.Equals("COM5", StringComparison.OrdinalIgnoreCase)
        || name.Equals("COM6", StringComparison.OrdinalIgnoreCase)
        || name.Equals("COM7", StringComparison.OrdinalIgnoreCase)
        || name.Equals("COM8", StringComparison.OrdinalIgnoreCase)
        || name.Equals("COM9", StringComparison.OrdinalIgnoreCase)
        || name.Equals("LPT1", StringComparison.OrdinalIgnoreCase)
        || name.Equals("LPT2", StringComparison.OrdinalIgnoreCase)
        || name.Equals("LPT3", StringComparison.OrdinalIgnoreCase)
        || name.Equals("LPT4", StringComparison.OrdinalIgnoreCase)
        || name.Equals("LPT5", StringComparison.OrdinalIgnoreCase)
        || name.Equals("LPT6", StringComparison.OrdinalIgnoreCase)
        || name.Equals("LPT7", StringComparison.OrdinalIgnoreCase)
        || name.Equals("LPT8", StringComparison.OrdinalIgnoreCase)
        || name.Equals("LPT9", StringComparison.OrdinalIgnoreCase))
        return false;
        
    return true;
}

For checking path names, you can use the similar Path.GetInvalidPathChars() method:

bool IsValidPathName(string name)
{
    // empty path is not allowed
    if (string.IsNullOrEmpty(name))
        return false;
    
    // check for characters that are not allowed in a path name
    if (Path.GetInvalidPathChars().Any(c => name.Contains(c))
        return false;
    
    // check the file name part
    if (!IsValidFileName(Path.GetFileName(name))
        return false;
        
    return true;
}
Klaus Gütter
  • 11,151
  • 6
  • 31
  • 36
0

I just dont want the <>:/|?*

To achieve this, you can try to use Regex to exclude them.

// if contains the characters, return true
bool containsChar = Regex.IsMatch(textBox1.Text, @"[<>:/|\\?*]");

but I also want the names like con and nul to not work

Do you want to exclude con and nul as filename, or do you not allow these substrings in the string? If the latter, you can refer to the code as followed.

// array to store excluded substring
string[] excludesubstring = { "con", "nul" };
bool containsSubstr = false;

foreach (string str in excludesubstring)
{
    if (containsSubstr = textBox1.Text.Contains(str) == true)
        break;
}

Last, use the following to check if it is a valid filename.

if (containsChar || containsSubstr)
{
    Console.WriteLine("filename includes invalid content");
}
大陸北方網友
  • 3,696
  • 3
  • 12
  • 37