C#操作文件夹、文件

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");
                    }
                }
            }
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值