停止前循环 [英] stoping forecahloop

查看:112
本文介绍了停止前循环的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

string[] drives = Directory.GetLogicalDrives();
            foreach (string str in drives)
            {
                int i = 0;
                string[] driv = Directory.GetDirectories(str);
              string s1 = str + "WINDOWS";//C:\Documents and Settings  WINDOWS
              //  string s1 = str + "XMLFile1.xml";
                foreach (string str1 in driv)
                {
                    if (str1.ToLower() == s1.ToLower())
                    {




这是我的问题,当驱动器为只读(cd rom或dvd rom)时,异常将引发,我想避免该驱动器该怎么办,请帮帮我




here my problem is when the drive is readonly(cd rom or dvd rom) that the exception will raising i want avoid that drives how can i do it please help me

推荐答案

您可以在Directory.GetDirectories调用周围使用try/catch.看来您正在尝试获取Windows文件夹,该文件夹可以通过以下更整洁的方式完成:
you could use try/catch around the Directory.GetDirectories call. It looks like you are trying to get the windows folder which can be done in a much neater way like this:
string windowsFolder = Environment.GetFolderPath(Environment.SpecialFolder.Windows);


这篇关于停止前循环的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆