C# 可视化读取文件、文件夹

时间:2025-03-28 10:35:19
OpenFileDialog fd = new OpenFileDialog();
fd.Filter = "txt files (*.txt)|*.txt|All files(*.*)|*.*";
fd.InitialDirectory = Application.StartupPath + "\\Temp\\";
fd.ShowReadOnly = true;
DialogResult r = fd.ShowDialog();
if(r == DialogResult.OK)
{
string all = fd.FileName;
string path = all.Substring(, all.LastIndexOf("\\") + );
string fileName = all.Substring(all.LastIndexOf("\\") + , all.LastIndexOf(".") - (all.LastIndexOf("\\") + ));
string fileExc = all.Substring(all.LastIndexOf(".") + , all.Length - all.LastIndexOf(".") - ); System.Console.WriteLine("all = {0}", all);
System.Console.WriteLine("path = {0}" ,path);
System.Console.WriteLine("fileName = {0}", fileName);
System.Console.WriteLine("fileExc = {0}",fileExc);
}
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.ShowNewFolderButton = true; if (fbd.ShowDialog() == DialogResult.OK)
{
string path = fbd.SelectedPath;
}
else
{
}
获取该路径下的文件目录
string[] dataFiles = Directory.GetFiles(path);