文件资源管理器 - UnauthorizedAccessException [英] File explorer - UnauthorizedAccessException

查看:48
本文介绍了文件资源管理器 - UnauthorizedAccessException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在sharpcorner上找到了这个简单的文件资源管理器,用于WPF.

I've found this simple File Explorer for WPF on sharpcorner.

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:例如,我会收到此错误:

The program works. But if I click on drive C: for example, I get this error:

System.UnauthorizedAccessException was unhandled

.

它涉及第一个 foreach:

It concerns the first foreach:

foreach (DirectoryInfo CDIR in DR.GetDirectories())

.

如果我以管理员身份启动程序,它就可以工作.但是我可以在没有管理员的情况下避免/解决这个问题吗?

If I start the program as administrator, it works. But can I avoid/solve this problem without being administrator?

推荐答案

你的问题出现较早:foreach (DirectoryInfo DR in DIR.GetDirectories())
C:\ 包含只有管理员权限才能访问的文件夹.您尝试获取此类文件夹的信息,但程序抛出异常,因为您无法获取此类信息.

Your problem occurs earlier: foreach (DirectoryInfo DR in DIR.GetDirectories())
C:\ contains folders which are only accessible with admin rights. You try to get infos on such folders and the program throws an exception because you are not allowed to get that kind of information.

可能的解决方法如下:

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

这篇关于文件资源管理器 - UnauthorizedAccessException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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