您的位置:首页 > 编程语言 > C#

C#中获取文件夹地址、获取文件地址、获取文件夹中的某种类型文件集合、获取某文件中某种类型文件数量操作方法!

2016-06-22 00:35 393 查看
C#中获取文件夹地址、获取文件地址、获取文件夹中的某种类型文件集合、获取某文件中某种类型文件数量操作方法!

1.获取文件夹地址

//获取文件夹地址
FolderBrowserDialog fbd = new FolderBrowserDialog();
fbd.Description = "The Description Of The Dialog";
if (fbd.ShowDialog() == DialogResult.OK)
{
string path = fbd.SelectedPath;
*******
*******
}


2.获取某个文件地址

//获取某个文件地址
OpenFileDialog ofd = new OpenFileDialog();
ofd.Filter = "(FileTypeName)|(*.YourFileType)";
ofd.Title = "Your File Dialog Title";
if (ofd.ShowDialog() == DialogResult.OK)
{
string path = ofd.FileName;
*******
*******
}


3.获取文件夹中某类文件

//获取文件夹中某类文件集合
DirectoryInfo directoryInfo = new DirectoryInfo(PicturePosTextBox.Text);
FileInfo[] fileInfo = directoryInfo.GetFiles("*.jpg");
foreach (FileInfo info in fileInfo)
string fullName = info.FullName;
*******
*******


4.获取某文件中某种类型文件数量

//获取某文件中某种类型文件数量
string fileType = "*.YourFileType";
string directoryPath = "********";
int fileNum = Directory.GetFiles(directoryPath, fileType).Length;


5.获取文件名/拓展名

string fileFullName = @"C:\Program Files\Microsoft Visual Studio 14.0\test.jpg";
string filename = Path.GetFileName(fileFullName ); //文件名"test.jpg"
string extension = Path.GetExtension(fileFullName );//扩展名 ".aspxjpg"
string fileNameWithoutExtension = Path.GetFileNameWithoutExtension(fileFullName );// 没有扩展名 "test"


本人正在学习中,其他方法有待后续更新。。。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  c# 文件