简体   繁体   English

获取C#中所有大于1gb的文件

[英]Obtain all files that are over 1gb in C#

I'm trying to get all the non system files on my computer to find out what files are over 1gb via a WinForms application .我正在尝试获取计算机上的所有非系统文件,以通过WinForms 应用程序找出哪些文件超过 1gb。

Here is the code I'm working with:这是我正在使用的代码:

        const long b500mb = 65536000;
        const long b1gb = 134217728;
        public Main()
        {
            InitializeComponent();
        }
        //https://unitconverter.io/gigabits/bytes/1
        private void Main_Load(object sender, EventArgs e)
        {
            var cDriveDirectories = new DirectoryInfo(@"C:\").GetDirectories()
                .Where(f => !f.Attributes.HasFlag(FileAttributes.System))
                .Where(w => w.FullName != @"C:\Windows")
                .Select(f => f.FullName)
                .ToList();

            var filesOver1Gb = new List<FileInfo>();

            foreach (var item in cDriveDirectories)
            {
                DirectoryInfo d = new DirectoryInfo(item);

                var files = d.GetFiles("*", SearchOption.AllDirectories).Where(w => w.Attributes.HasFlag(FileAttributes.Normal)
                                                                                 && w.Length > b1gb).ToList();

                foreach (FileInfo file in files)
                {
                    filesOver1Gb.Add(file);
                }
            }
        }

How can I get around this error?我怎样才能解决这个错误?

System.UnauthorizedAccessException: 'Access to the path 'C:\.netpub\history' is denied.' System.UnauthorizedAccessException:“拒绝访问路径‘C:\.netpub\history’。”

After @zee's help:在@zee 的帮助下:

I VS as an admin and then put a try/catch around the line of code that was failing and continued.我以管理员身份进行 VS,然后在失败并继续的代码行周围放置一个 try/catch。

This code will obtain the information I need!这段代码将获取我需要的信息!

        const long b500mb = 65536000;
        const long b1gb = 134217728;
        public Main()
        {
            InitializeComponent();
        }
        //https://unitconverter.io/gigabits/bytes/1
        private void Main_Load(object sender, EventArgs e)
        {
            var cDriveDirectories = new DirectoryInfo(@"C:\").GetDirectories()
                .Where(f => !f.Attributes.HasFlag(FileAttributes.System))
                .Where(w => w.FullName != @"C:\Windows")
                .Select(f => f.FullName)
                .ToList();

            var filesOver1Gb = new List<FileInfo>();
            IList<string> unauthorizedFiles = new List<string>();

            foreach (var item in cDriveDirectories)
            {
                DirectoryInfo d = new DirectoryInfo(item);

                var files  = new List<FileInfo>();
                try
                {
                    files = d.GetFiles("*", SearchOption.AllDirectories).Where(w => !w.Attributes.HasFlag(FileAttributes.System)
                                                                                     && w.Length > b1gb).ToList();
                }
                catch (UnauthorizedAccessException)
                {
                    // ignore error and continue to process files over 1gb.
                }


                foreach (FileInfo file in files)
                {
                    filesOver1Gb.Add(file);
                }
            }

            // get the total count in bytes to find out how many gbs we have etc.
            long totalOver1Gb = 0;
            foreach (var file in filesOver1Gb)
            {
                totalOver1Gb = totalOver1Gb + file.Length;
            }
        }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM