C#操作文件夹、文件
//获得文件名
Console.WriteLine(Path.GetFileName(str));
//获得文件名但是不包含扩展名
Console.WriteLine(Path.GetFileNameWithoutExtension(str));
//获得文件的扩展名
Console.WriteLine(Path.GetExtension(str));
//获得文件所在的文件夹的名称
Console.WriteLine(Path.GetDirectoryName(str));
//获得文件所在的全路径
Console.WriteLine(Path.GetFullPath(str));
//连接两个字符串作为路径
Console.WriteLine(Path.Combine(@"c:\a\" , "b.txt"));
```csharp
private void btnSelect_Click(object sender, EventArgs e)
{
//打开文件夹对话框
FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
folderBrowserDialog.Description = "请选择文件路径";//文件夹title
if (folderBrowserDialog.ShowDialog()==DialogResult.OK)
{
//获取文件夹完整路径
string foldPath = folderBrowserDialog.SelectedPath;
this.txtPath.Text = foldPath;
//MessageBox.Show(foldPath);
}
}
```csharp
private void txtSearch_Click(object sender, EventArgs e)
{
string strPath = this.txtPath.Text.Trim();
if (string.IsNullOrEmpty(strPath))
{
MessageBox.Show("路径未选择", "提示");
return;
}
//将字符串文件夹绝对路径,转化为路径
DirectoryInfo rootPath = new DirectoryInfo(strPath);
//获取该文件夹路径下的所有文件
FileInfo[] dicList = rootPath.GetFiles();
foreach (FileInfo item in dicList)
{
//获取文件的扩展文件名
string fileExten = Path.GetExtension(item.ToString());
if (string.Equals(".lib", fileExten))
{
//如果结束符,.前面为d为debug GetFileNameWithoutExtension 获取不带扩展名的文件名
if (Path.GetFileNameWithoutExtension(item.ToString()).EndsWith("d"))
{
this.txtDebug.AppendText(item.ToString() + "\r\n");
}
else
{
this.txtRelease.AppendText(item.ToString() + "\r\n");
}
}
}
}