C#判断文件夹存在指定文件

版权声明:欢迎转载,请注明出处 https://blog.csdn.net/u012388993/article/details/82876130

有时候会需要用到判断文件夹是否有某文件,这里描述一下方法


 

举例一下,比如我需要判断avrdude文件夹是否有在我的程序的同一个目录里,那么

if (Directory.Exists(@".\avrdude") == true)//如果有这个文件夹就提示
            {
                tbInfo.Text = "Avrdude正常,等待操作";
            }
            else
            {
                tbInfo.Text = "Avrdude异常,请检查";
            }

(调用Directory需要引用System.IO)

这里要注意的是,如果是调试模式下需要把文件夹放入到Debug文件夹中

进一步,如果要继而判断有没有指定的文件,那么就写为

if (Directory.Exists(@".\avrdude") == true)//如果有这个文件夹就提示
            {
               if( File.Exists(@".\avrdude\avrdude.exe") == true)
                    tbInfo.Text = "Avrdude正常,等待操作";
               else
                {
                    tbInfo.Text = "Avrdude异常,请检查";
                }
            }
            else
            {
                tbInfo.Text = "Avrdude异常,请检查";
            }

猜你喜欢

转载自blog.csdn.net/u012388993/article/details/82876130