C # Get file name and extension

Source: Internet
Author: User

Reprinted from Http://www.cnblogs.com/libushuang/p/5794976.html

C # Get file name and extension

String afirstname = Afile.substring (afile.lastindexof ("\ \") + 1, (Afile.lastindexof (".")-afile.lastindexof ("\ \")-1))  ;   File name 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.

There is also the use of 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 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)//return without extension File name System.IO.Path.GetDirectoryName (filePath)//Returns 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 file name and extension

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.