SoFunction
Updated on 2025-04-06

Implementation of obtaining various file names in C#

C# Get various file names

1. C# obtains the file name with suffix, suffix name, and file name according to the absolute path.

 string str =" E:\test\";
                string filename = (str);//file name ""                string extension = (str);//Extension ".aspx"                string fileNameWithoutExtension = (str);// Filename without extension “Default”

2. C# obtains the file name, suffix name, and file name with the absolute path, and use the Split function.

 string str = =" E:\test\";
                char[] delimiterChars = { '.', '\\' };
                string[] Mystr = (delimiterChars);
                string sheetName = Mystr[ - 2];);// Filename without extension “Default”

3. C# get file name and extension

string aFirstName = (("\\") + 1, ((".") - ("\\") - 1));  //file namestring aLastName = ((".") + 1, ( - (".") - 1));   //Extension

Something else is to use Substring to intercept

 (("\\") + 1,  - 1 - ("\\"));
 (("."),  - ("."));

Or use

This will allow you to get the directory path where the file is located

string path1 = () + @"\";
string path = ("C:\My Document\path\");    //Get only file names
string fullPath = @"\WebSite1\";
string filename = (fullPath);//file name  ""string extension = (fullPath);//Extension ".aspx"string fileNameWithoutExtension = (fullPath);// Filename without extension “Default”
(filePath)       //Return the file name with extension(filePath)     //Return file name without extension(filePath)     //Return to the directory where the file is located

4. Other methods

  • a. Get the full path of the current process, including the file name (process name).
string str = ().;
result: X:\xxx\xxx\ (.exeThe directory where the file resides+.exefile name)
  • b. Get the full path of the main module to which the new Process component is associated with the currently active process, including the file name (process name).
string str = ().;
result: X:\xxx\xxx\ (.exeThe directory where the file resides+.exefile name)
  • c. Get and set the fully qualified path to the current directory (i.e. the directory from which the process starts).
string str = ;
result: X:\xxx\xxx (.exeThe directory where the file resides)
  • d. Get the base directory of the current application domain of the current Thread, which is used by the assembly conflict resolver to detect the assembly.
string str = ;
result: X:\xxx\xxx\ (.exeThe directory where the file resides+"\")
  • e. Get and set the name of the directory containing the application.
string str = ;
result: X:\xxx\xxx\ (.exeThe directory where the file resides+"\")
  • f. Get the path to the executable file that started the application, excluding the name of the executable file.
string str = ;
result: X:\xxx\xxx (.exeThe directory where the file resides)
  • g. Get the path to the executable file that started the application, including the name of the executable file.
string str = ;
result: X:\xxx\xxx\ (.exeThe directory where the file resides+.exefile name)

C# Get all folder names in the specified folder

 public static string[] GetFilePath(string path)
    {
        if ((path))
        {
            //File path            string[] dir = (path);
            //file name            string[] names = new string[];
            for (int i = 0; i < ; i++)
            {
                //Name of the assignment file                names[i] = (dir[i]);
            }
            return names;
        }
        else
        {
            ("Path not found");
            return null;
        }
    }

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.