文件资源管理器 - UnauthorizedAccessException

时间:2014-06-04 12:25:43

标签: c# .net wpf xaml

我在sharpcorner上为WPF找到了这个简单的File Explorer

namespace WindowsExplorer
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.cmbDrive.ItemsSource = DriveInfo.GetDrives().Where(dr => dr.IsReady == true).ToList();
            this.cmbDrive.DisplayMemberPath = "Name";
            this.cmbDrive.SelectedValuePath = "Name";
        }

        private void cmbDrive_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.tvwDirectory.Items.Clear();
            DirectoryInfo DIR = new DirectoryInfo(this.cmbDrive.SelectedValue.ToString());

            foreach (DirectoryInfo DR in DIR.GetDirectories())
            {
                MyTreeViewItem TVI = new MyTreeViewItem();
                TVI.Header = DR.Name;
                TVI.Path = DR.FullName;
                TVI.Type = DR.GetType().Name;
                TVI.Expanded += new RoutedEventHandler(CTVI_Expanded);
                if (!DR.Attributes.ToString().Contains("Hidden"))
                {
                    foreach (DirectoryInfo CDIR in DR.GetDirectories())
                    {
                        MyTreeViewItem CTVI = new MyTreeViewItem();
                        CTVI.Expanded += new RoutedEventHandler(CTVI_Expanded);
                        CTVI.Header = CDIR.Name;
                        CTVI.Path = CDIR.FullName;
                        CTVI.Type = CDIR.GetType().Name;
                        TVI.Items.Add(CTVI);

                    }
                    this.tvwDirectory.Items.Add(TVI);
                }
            }
            foreach (FileInfo FL in DIR.GetFiles())
            {

                this.ltbExplorer.Items.Add(FL.Name);
            }
        }

        void CTVI_Expanded(object sender, RoutedEventArgs e)
        {
            MyTreeViewItem TVI = (MyTreeViewItem)sender;

            foreach (MyTreeViewItem CTVI in TVI.Items)
            {
                if (CTVI.Type == "DirectoryInfo")
                {
                    DirectoryInfo DIR = new DirectoryInfo(CTVI.Path);
                    foreach (DirectoryInfo CDIR in DIR.GetDirectories())
                    {
                        MyTreeViewItem CTVI1 = new MyTreeViewItem();
                        CTVI1.Expanded += new RoutedEventHandler(CTVI_Expanded);
                        CTVI1.Header = CDIR.Name;
                        CTVI1.Path = CDIR.FullName;
                        CTVI1.Type = CDIR.GetType().Name;
                        if (CTVI.Items.Contains(CTVI1.Header) == false)
                            CTVI.Items.Add(CTVI1);
                    }
                }
            }
            e.Handled = true;
        }
        private void tvwDirectory_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue != null)
            {
                this.ltbExplorer.Items.Clear();
                MyTreeViewItem TVI = (MyTreeViewItem)e.NewValue;
                DirectoryInfo DIR = new DirectoryInfo(TVI.Path);
                foreach (FileInfo FL in DIR.GetFiles())
                {
                    ltbExplorer.Items.Add(FL.Name);
                }
            }
            e.Handled = true;
        }
    }
}

namespace WindowsExplorer
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }

        private void Window_Loaded(object sender, RoutedEventArgs e)
        {
            this.cmbDrive.ItemsSource = DriveInfo.GetDrives().Where(dr => dr.IsReady == true).ToList();
            this.cmbDrive.DisplayMemberPath = "Name";
            this.cmbDrive.SelectedValuePath = "Name";
        }

        private void cmbDrive_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            this.tvwDirectory.Items.Clear();
            DirectoryInfo DIR = new DirectoryInfo(this.cmbDrive.SelectedValue.ToString());

            foreach (DirectoryInfo DR in DIR.GetDirectories())
            {
                MyTreeViewItem TVI = new MyTreeViewItem();
                TVI.Header = DR.Name;
                TVI.Path = DR.FullName;
                TVI.Type = DR.GetType().Name;
                TVI.Expanded += new RoutedEventHandler(CTVI_Expanded);
                if (!DR.Attributes.ToString().Contains("Hidden"))
                {
                    foreach (DirectoryInfo CDIR in DR.GetDirectories())
                    {
                        MyTreeViewItem CTVI = new MyTreeViewItem();
                        CTVI.Expanded += new RoutedEventHandler(CTVI_Expanded);
                        CTVI.Header = CDIR.Name;
                        CTVI.Path = CDIR.FullName;
                        CTVI.Type = CDIR.GetType().Name;
                        TVI.Items.Add(CTVI);
                    }
                    this.tvwDirectory.Items.Add(TVI);
                }
            }
            foreach (FileInfo FL in DIR.GetFiles())
            {

                this.ltbExplorer.Items.Add(FL.Name);
            }
        }

        void CTVI_Expanded(object sender, RoutedEventArgs e)
        {
            MyTreeViewItem TVI = (MyTreeViewItem)sender;


            foreach (MyTreeViewItem CTVI in TVI.Items)
            {
                if (CTVI.Type == "DirectoryInfo")
                {
                    DirectoryInfo DIR = new DirectoryInfo(CTVI.Path);
                    foreach (DirectoryInfo CDIR in DIR.GetDirectories())
                    {
                        MyTreeViewItem CTVI1 = new MyTreeViewItem();
                        CTVI1.Expanded += new RoutedEventHandler(CTVI_Expanded);
                        CTVI1.Header = CDIR.Name;
                        CTVI1.Path = CDIR.FullName;
                        CTVI1.Type = CDIR.GetType().Name;
                        if (CTVI.Items.Contains(CTVI1.Header) == false)
                            CTVI.Items.Add(CTVI1);
                    }
                }
            }
            e.Handled = true;
        }

        private void tvwDirectory_SelectedItemChanged(object sender, RoutedPropertyChangedEventArgs<object> e)
        {
            if (e.NewValue != null)
            {
                this.ltbExplorer.Items.Clear();
                MyTreeViewItem TVI = (MyTreeViewItem)e.NewValue;
                DirectoryInfo DIR = new DirectoryInfo(TVI.Path);
                foreach (FileInfo FL in DIR.GetFiles())
                {
                    //MyTreeViewItem CTVI2 = new MyTreeViewItem();
                    //CTVI2.Expanded += new RoutedEventHandler(CTVI_Expanded);
                    //CTVI2.Header = FL.Name;
                    //CTVI2.Path = FL.FullName;
                    //CTVI2.Type = FL.GetType().Name;
                    ltbExplorer.Items.Add(FL.Name);
                }
            }
            e.Handled = true;
        }
    }
}

该计划有效。但是,如果我点击驱动器C:例如,我收到此错误:

System.UnauthorizedAccessException was unhandled

它涉及第一个foreach:

foreach (DirectoryInfo CDIR in DR.GetDirectories())

如果我以管理员身份启动该程序,则可以正常运行。但是,如果不是管理员,我可以避免/解决这个问题吗?

1 个答案:

答案 0 :(得分:2)

您的问题发生得更早:foreach (DirectoryInfo DR in DIR.GetDirectories())
C:\包含仅可通过管理员权限访问的文件夹。 您尝试在此类文件夹上获取信息,并且该程序会抛出异常,因为您不允许获取此类信息。

可能的解决方法如下:

try
{
    foreach (DirectoryInfo DR in DIR.GetDirectories())
    {
        // stuff
    }
}
catch(exception ex)
{
    //do something with the failure. maybe ignore it
}