Home > Net >  C # separation file path, file name and file name extension
C # separation file path, file name and file name extension

Time:09-15

Private void button1_Click (object sender, EventArgs e)
{
OpenFileDialog OFD=new OpenFileDialog ();
DialogResult Dr=OFD. ShowDialog ();
If (Dr==DialogResult. OK)
{

MessageBox. Show (" select file road king is: "+ OFD. The FileName +" \ r \ n can add the code to implement the follow-up operation has been ");
String P_str_all=OFD. FileName;//get full file path of the selected
String P_str_path=P_str_all. Substring (0, P_str_all LastIndexOf (" \ \ ") + 1);//for complete file path

String P_str_filename=P_str_all. Substring (P_str_all LastIndexOf (" \ \ ") + 1, P_str_all. LastIndexOf (" ") - (P_str_all. LastIndexOf (" \ \ ") + 1));//get the file name
String P_str_fileexc=P_str_all. Substring (P_str_all LastIndexOf (". ") + 1, P_str_all. Length - P_str_all. LastIndexOf (" ") - 1);

Label1. Text="file path:" + P_str_path;

Label2. Text="file name:" + P_str_filename;

Label3. Text="file extension:" + P_str_fileexc;


}
The else
{
MessageBox. Show (" did not select any file ");

}

CodePudding user response:

https://docs.microsoft.com/zh-cn/dotnet/api/system.io.fileinfo? Redirectedfrom=MSDN& View=netframework 4.8

Learning c # to learn basic framework objects,

CodePudding user response:

Not so complicated, can try the FileInfo object

CodePudding user response:

The fileinfo also didn't see that

CodePudding user response:

The code is as follows:
 
using System.IO;

String filePath=@ "F: \ test. TXT";//the path name
Path. The GetFileName (filePath);//the result: the test. TXT
Path. GetFileNameWithoutExtension (filePath);//the result: the test
Path. GetExtension (filePath);//the result: TXT
  •  Tags:  
  • C#
  • Related