{
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.8Learning c # to learn basic framework objects,
CodePudding user response:
Not so complicated, can try the FileInfo objectCodePudding user response:
The fileinfo also didn't see thatCodePudding 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