我有一段代码,是为了判断两个文件的大小,
System.IO.FileInfo info = new System.IO.FileInfo("Start.exe");
System.IO.FileInfo info1 = new System.IO.FileInfo("data\\texts_us.tbl");    if (info.Length.ToString() == "4391424" && info1.Length.ToString() == "46371")
    {
    }
        else
    {    }
上面的写法是正常,现在问题是我想增加两个,就是判断四个文件的大小,请问要怎么写了。

解决方案 »

  1.   

    把文件放到一个IList<FileInfo>中,然后foreach()去比较,
      

  2.   

    public bool IsFileOk(string str, int length)
            {
                if (string.IsNullOrEmpty(str) || !File.Exists(str))
                {
                    throw new ArgumentException("Invalid path specified!");
                }            FileInfo info = new FileInfo(str);
                return info.Length == length;
            }        // demo code
            
                string file1 = "Start.exe";
                int len1 = 4391424;
                bool isFile1OK = IsFileOk(file1, len1);
            
      

  3.   

    那你可以把这四个都放在if语句中啊,那样如果不是同时满足,就不能成立!
    System.IO.FileInfo info = new System.IO.FileInfo("Start.exe"); 
    System.IO.FileInfo info1 = new System.IO.FileInf("data\\texts_us.tbl"); 
    System.IO.FileInfo info2 = new System.IO.FileInfo("Start1.exe"); 
    System.IO.FileInfo info3 = new System.IO.FileInf("data\\texts_us1.tbl");
        if (info.Length.ToString() == "4391424" && info1.Length.ToString() == "46371" && info2.Length.ToString() == "45615" && info3.Length.ToString() == "45147") 
        {     } 
            else 
        {     } 
      

  4.   

    把大小添加到list<int,int>泛型里,循环判断
      

  5.   

    定义一个实现IComparable 接口的类.using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Collections;
    using System.IO;namespace 通过IComparable是类型可排序
    {
        public  class CompareFileSize : IComparer 
        {
            //通过重载 Compare 可以定制比较方式. 此处.为比较文件大小.顺序排列为从小到大..
            //(如果想从大到小显示只需将return 1 ,-1 调换下就可以)        
            public int Compare(object o1, object o2)
            {
                if (!(o1 is FileInfo) || !(o2 is FileInfo))
                {
                    throw new Exception(" Is Not The FileInfo Object !");
                }
                if (((FileInfo)o1).Length == ((FileInfo)o2).Length)
                {
                    return 0;
                }
                else if (((FileInfo)o1).Length > ((FileInfo)o2).Length)
                    return 1;
                else
                    return -1;
            }
        }
    }
    using System;
    using System.Collections.Generic;
    using System.Text;
    using System.Data;
    using System.Collections;
    using System.IO;namespace 通过IComparable是类型可排序
    {
        class Program
        {
            static void Main(string[] args)
            {
             // 用实现的接口比较大小,方便且不限制比较的数目.
                CompareFileSize comparefilesize = new CompareFileSize ();
                FileInfo[] fileinfo = new FileInfo[4];            
                fileinfo[0] = new FileInfo (@"d:\test01.txt") ;
                fileinfo[1] = new FileInfo (@"d:\test02.txt")  ;
                fileinfo[2] = new FileInfo (@"d:\test03.txt") ;
                fileinfo[3] = new FileInfo (@"d:\test04.txt");            Array.Sort(fileinfo,comparefilesize);
                foreach (FileInfo i in fileinfo)
                {
                    Console.WriteLine(i.Name.ToString());
                }
                Console.ReadLine();
            }
        }
    }