C#语言项目 [英] Project in C# language

查看:47
本文介绍了C#语言项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我想要一个可以在一个驱动器(例如,计算机中的驱动器D)中检查NTFS文件系统的程序.调查结果必须按3组显示和计数所有文件和程序.
group1:文件大小0-10千字节.
group2:文件大小10-1000千字节.
group3:文件大小1000-100000千字节.

该项目的主要重点是计算上述三个3组中Drive(示例)的文件大小.

Hi,

I would like a program that surveys a NTFS file system in one drive, for example, drive D in your computer. The survey results must show and count all of files and programs in 3 groups.
group1 : size of files 0 - 10 kilo bytes.
group2 : size of files 10 - 1000 kilo bytes.
group3 : size of files 1000 - 100000 kilo bytes.

The main focus of this project is counting file size of Drive(example) in 3 three groups, that above-mentioned.

推荐答案

这似乎是问题所在,没有人正在阻止您编写所需的程序/代码.

使用Google来获取想法并开始编码.

一旦有了代码并遇到问题,您总是可以带着一个更具体的问题回到这里,社区将竭尽所能为您提供帮助.不要忘了只张贴引起问题的相关代码位,因为代码转储通常根本无法帮助别人帮助/帮助您.

祝您编程愉快.
What seems to be the problem, nobody is stopping you writing the program/code you want.

Use Google to get ideas and start coding.

Once you have code and run into problems you can always come back here with a more specific question and the community will do its best to help you. Don''t forget to post only the relevant code bits that pose the problem as a code dump is not usually helpful at all in getting someone to help/assist you.

Good luck and happy coding.


递归是您的解决方案:

Recursion is your solution :

class Program
    {
        static void Main(string[] args)
        {
            DirectoryInfo tDir = new DirectoryInfo(@"C:\");
            string Pattern = "a";
             TraverseDirs(tDir, Pattern);
            Console.Read();
        }
         private static void TraverseDirs(DirectoryInfo dir, string Pattern)
         {            // Subdirs
            try         // Avoid errors such as "Access Denied"
            {
                foreach (DirectoryInfo iInfo in dir.GetDirectories())
                {
                    if (iInfo.Name.StartsWith(Pattern))
                        Console.WriteLine("Found dir:  " + iInfo.FullName);
                     TraverseDirs(iInfo, Pattern);
                }
            }
            catch (Exception)
            {
            }
             // Subfiles
            try         // Avoid errors such as "Access Denied"
            {
                foreach (FileInfo iInfo in dir.GetFiles())
                {
                    if (iInfo.Name.StartsWith(Pattern))
                        Console.WriteLine("Found file: " + iInfo.FullName);
                }
            }
            catch (Exception)
            {
            }
        }    
}



这不完全是您想要的,但我希望您可以基于此来弄清楚,下面还会提供一些参考.

http://www.dotnetperls.com/recursively-find-files [ http://msdn.microsoft.com/en-us/library/bb513869.aspx [ ^ ]

http://rosettacode.org/wiki/Walk_a_directory/Recursively#C.23 [ ^ ]



This is not exactly what you want, but I hope you can figure out based on this, some reference is also provided below.

http://www.dotnetperls.com/recursively-find-files[^]

http://msdn.microsoft.com/en-us/library/bb513869.aspx[^]

http://rosettacode.org/wiki/Walk_a_directory/Recursively#C.23[^]


您问过对于代码?就是这样:(只需复制到控制台项目,并用以下代码替换Program.cs)
(请记住,驱动器上的文件大小不会像1kB = 1000B那样被测量,而是1kB = 1024B,...).
在包含大量文件的大型驱动器上,计数可能需要一些时间!

You asked for code? Here is it: (just copy to a console project and replace Program.cs with the following code)
(Keep in mind that file sizes on drives are not meassured like 1kB = 1000B but 1kB = 1024B, ...)
Counting can take some time on big drives with a lot of files!

using System;
using System.IO;

namespace CountFiles
{
    class Program
    {
        static void Main(string[] args)
        {
            string strRootPath = @"D:\";
            int iCount10kB = 0,iCount1000kB = 0,iCount100000kB = 0, iCountBigger = 0;
            CountFiles(strRootPath,
                ref iCount10kB, ref iCount1000kB, ref iCount100000kB, ref iCountBigger);
            Console.WriteLine("Results for root directory {0}", strRootPath);
            Console.WriteLine("Files 0kB - 10 kB:           {0}", iCount10kB);
            Console.WriteLine("Files 10kB - 1000 kB:        {0}", iCount1000kB);
            Console.WriteLine("Files 1000kB - 100000 kB:    {0}", iCount100000kB);
            Console.WriteLine("Files bigger than 100000 kB: {0}", iCountBigger);
            Console.ReadKey();
        }

        static void CountFiles(string strRootPath,
            ref int iCount10kB, ref int iCount1000kB, ref int iCount100000kB, ref int iCountBigger)
        {
            try
            {
                string[] astrFiles = Directory.GetFiles(strRootPath);
                foreach (string strFile in astrFiles)
                {
                    long iFileLength = new FileInfo(strFile).Length;
                    if (iFileLength < 10000)
                        iCount10kB++;
                    else if (iFileLength < 1000000)
                        iCount1000kB++;
                    else if (iFileLength < 100000000)
                        iCount100000kB++;
                    else
                        iCountBigger++;
                }
            }
            catch
            {
                // ignore file access errors
            }

            try
            {
                string[] astrSubDirs = Directory.GetDirectories(strRootPath);
                foreach (string strSubDir in astrSubDirs)
                {
                    CountFiles(strSubDir, // recursion
                        ref iCount10kB, ref iCount1000kB, ref iCount100000kB, ref iCountBigger);
                }
            }
            catch
            {
                // ignore directory access errors
            }
        }
    }
}


这篇关于C#语言项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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