简体   繁体   中英

How to check for illegal file/folder names in c#

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.

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;
}

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");
}

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