1. C # Gets the suffix file name, suffix name, and file name according to the absolute path.
String str = "E:\test\Default.aspx";
string filename = System.IO.Path.GetFileName (str);//filename "default.aspx"
string extension = System.IO.Path.GetExtension (str);//extension ". aspx"
String filenamewithoutextension = System.IO.Path.GetFileNameWithoutExtension (str);//File name "Default" with no extension
2. C # Gets the suffix file name, suffix name, and file name, using the Split function, based on the absolute path.
string str = = "E:\test\Default.aspx";
Char[] Delimiterchars = {'. ', ' \ \ '};
string[] mystr = str. Split (Delimiterchars);
String sheetname = mystr[mystr.length-2];);//File name "Default" with no extension
Here is my reprint, retained.
C # Get the file name and extension (reprinted from:http://www.cnitblog.com/yide/archive/2012/03/08/78003.aspx)
String afirstname = Afile.substring (afile.lastindexof ("\ \") + 1, (Afile.lastindexof (".")-afile.lastindexof ("\ \")-1)) ; Filename
String alastname = Afile.substring (Afile.lastindexof (".") + 1, (Afile.length-afile.lastindexof (".")-1)); Extended Name
String strfilepaht= "file path";
Path.getfilenamewithoutextension (strFilePath); This is what gets the file name.
And there's the substring intercept.
Strfilepaht.substring (path. LastIndexOf ("\ \") + 1, path. Length-1-Path. LastIndexOf ("\ \"));
Strfilepaht.substring (path. LastIndexOf ("."), path. Length-path. LastIndexOf ("."));
or with Openfiledialog1.safefilename.
This will take you to the directory path where the file is located.
String path1 = System.IO.Path.GetDirectoryName (openfiledialog1.filename) + @ "\";
String path = Path.getfilename ("C:\My document\path\image.jpg"); Get file name only Image.jpg
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////
String fullPath = @ "\website1\default.aspx";
string filename = System.IO.Path.GetFileName (fullPath);//filename "default.aspx"
string extension = System.IO.Path.GetExtension (FullPath);//extension ". aspx"
String filenamewithoutextension = System.IO.Path.GetFileNameWithoutExtension (FullPath);//File name "Default" with no extension
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////
System.IO.Path.GetFileNam (FilePath)//Return file name with extension
System.IO.Path.GetFileNameWithoutExtension (FilePath)//Returns a file name without an extension
System.IO.Path.GetDirectoryName (FilePath)//Return to the directory where the file is located
/////////////////////////////////////////////////////////////////////////////////////////////////////////////// //////////////////////////////
Gets the full path of the current process, including the file name (process name).
String str = this. GetType (). Assembly.location;
Result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)
Gets the full path of the new process component and its main module associated with the currently active process, including the file name (process name).
String str = System.Diagnostics.Process.GetCurrentProcess (). Mainmodule.filename;
Result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)
Gets and sets the fully qualified path to the current directory (that is, the directory from which the process starts).
string str = System.Environment.CurrentDirectory;
Result:x:\xxx\xxx (the directory where the. exe file resides)
Gets the base directory of the current application domain for Thread, which is used by the Assembly resolver to probe assemblies.
string str = System.AppDomain.CurrentDomain.BaseDirectory;
Result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")
Gets and sets the name of the directory that contains the application.
string str = System.AppDomain.CurrentDomain.SetupInformation.ApplicationBase;
Result:x:\xxx\xxx\ (the directory where the. exe file is located + "\")
Gets the path to the executable file that launched the application, not including the name of the executable file.
string str = System.Windows.Forms.Application.StartupPath;
Result:x:\xxx\xxx (the directory where the. exe file resides)
Gets the path to the executable file that launched the application, including the name of the executable file.
string str = System.Windows.Forms.Application.ExecutablePath;
Result:x:\xxx\xxx\xxx.exe (. exe file in the same directory as +.exe filename)
Gets the current working directory of the application (unreliable).
String str = System.IO.Directory.GetCurrentDirectory ();
Result:x:\xxx\xxx (the directory where the. exe file resides)
C # get various file names