匹配文件的名称和大小

时间:2012-08-01 06:18:48

标签: c# .net wpf

我在整合两段代码时遇到了一些麻烦。第一个检查文件的大小,下一个文件循环通过SQL数据库并查找文件的匹配名称。我基本上想检查它是否是一个新文件,或者文件是否因为我上次记录了一些数据而发生了变化。

这将获取目录

中每个文件的大小
 // Make a reference to a directory.
        DirectoryInfo di = new DirectoryInfo("C:\\Users");
        // Get a reference to each file in that directory.
        FileInfo[] fiArr = di.GetFiles();
        // Display the names and sizes of the files.
        MessageBox.Show("The directory {0} contains the following files:", di.Name);
        foreach (FileInfo f in fiArr)
            MessageBox.Show("The size of" + f.Name + " is " + f.Length + " bytes.");

此代码循环,直到它找到一个马赫或直到所有条目都已被查看。

  try
            {
                // LINQ query for all files containing the word '.txt'.
                var files = from file in
                                Directory.EnumerateFiles("C:\\Users")
                            where file.ToLower().Contains(".txt")
                            select file;

            foreach (var file in files)
            {
                //Get path to HH file
                filename = System.IO.Path.GetFileName(file);

                tempString = "";

                //Keep looking trough database utill database empty or HH found
                while (inc != numberOfSessions && (filename != tempString))
                { 

                    sessionRow = sessions.Tables["Sessions"].Rows[inc];
                    tempString = sessionRow.ItemArray.GetValue(1).ToString();

                    inc++;
                }

让我们说ItemAttay.GetValue(2)返回文件的保存大小。如果

,我怎样才能最有效地保持while循环

inc!= numberOfSessions&& (filename!= tempString)&& (sessionRow.ItemArray.GetValue(2)== f.length)

感谢您一看!

1 个答案:

答案 0 :(得分:0)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using System.Data;



    class Program
    {
        static void Main(string[] args)
        {
            var files1 = new List<string>(Directory.GetFiles(args[0],
                "*.txt",
                SearchOption.AllDirectories));


            List<FileData> ListFiles = new List<FileData>();

            for (int i = 0; i < files1.Count; i++)
            { 


            FileInfo file = new FileInfo(files1[i]);
            FileData _tmpfile = new FileData(file.Name.ToString(), file.Length, 
                File.GetLastWriteTime(files1[1]).ToString("yyyy-MM-dd H:mm:ss"),
                File.GetLastAccessTime(files1[1]).ToString("yyyy-MM-dd H:mm:ss"));
            ListFiles.Add(_tmpfile);
            }

            DataSet sessions = new DataSet();
            DataTable dt = sessions.Tables["Sessions"];
            for (int i = 0; i < ListFiles.Count; i++)
            {
                //compares every file in folder to database
                FileData _tmp = ListFiles[i];
                for (int j = 0; j < dt.Rows.Count; j++)
                {
                    if (_tmp.GSFileName == dt.Rows[i][0].ToString())
                    { 
                        //put some code here
                        break; 
                    }

                    if (_tmp.GSSize == long.Parse(dt.Rows[i][1].ToString()))
                    {
                        //put some code here
                        break;
                    }

                }


            }



        }
    }
    public class FileData
    {
        string FileName = "";

        public string GSFileName
        {
            get { return FileName; }
            set { FileName = value; }
        }
        long Size = 0;

        public long GSSize
        {
            get { return Size; }
            set { Size = value; }
        }
        string DateOfModification = "";

        public string GSDateOfModification
        {
            get { return DateOfModification; }
            set { DateOfModification = value; }
        }
        string DateOfLastAccess = "";

        public string GSDateOfLastAccess
        {
            get { return DateOfLastAccess; }
            set { DateOfLastAccess = value; }
        }

        public FileData(string fn, long si, string dateofmod, string dateofacc)
        {
            FileName = fn;
            Size = si;
            DateOfModification = dateofmod;
            DateOfLastAccess = dateofacc;

        }

    }