zoukankan      html  css  js  c++  java
  • windowsAPI遍历文件夹(速度高于递归)

    #region API 遍历文件夹及其子文件夹和子文件

    #region 声明WIN32API函数以及结构 **************************************
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern IntPtr FindFirstFile(string pFileName, ref Win32FindData pFindFileData);
    [DllImport("kernel32.dll", CharSet = CharSet.Auto, SetLastError = true)]
    private static extern bool FindNextFile(IntPtr hndFindFile, ref Win32FindData lpFindFileData);
    [DllImport("kernel32.dll", SetLastError = true)]
    private static extern bool FindClose(IntPtr hndFindFile);

    private int FILE_SHARE_READ = 0x00000001;
    private int FILE_SHARE_WRITE = 0x00000002;
    private int FILE_SHARE_DELETE = 0x00000004;
    private int FILE_ATTRIBUTE_READONLY = 0x00000001;
    private int FILE_ATTRIBUTE_HIDDEN = 0x00000002;
    private int FILE_ATTRIBUTE_SYSTEM = 0x00000004;
    private int FILE_ATTRIBUTE_DIRECTORY = 0x00000010;
    private int FILE_ATTRIBUTE_ARCHIVE = 0x00000020;
    private int FILE_ATTRIBUTE_DEVICE = 0x00000040;
    private int FILE_ATTRIBUTE_NORMAL = 0x00000080;
    private int FILE_ATTRIBUTE_TEMPORARY = 0x00000100;
    private int FILE_ATTRIBUTE_SPARSE_FILE = 0x00000200;
    private int FILE_ATTRIBUTE_REPARSE_POINT = 0x00000400;
    private int FILE_ATTRIBUTE_COMPRESSED = 0x00000800;
    private int FILE_ATTRIBUTE_OFFLINE = 0x00001000;
    private int FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 0x00002000;
    private int FILE_ATTRIBUTE_ENCRYPTED = 0x00004000;
    private int FILE_ATTRIBUTE_INTEGRITY_STREAM = 0x00008000;
    private int FILE_ATTRIBUTE_NO_SCRUB_DATA = 0x00020000;
    #endregion

    //具体参数方法函数
    readonly Stack<KeyValuePair<string, string>> _mScopes = new Stack<KeyValuePair<string, string>>();
    private static readonly IntPtr InvalidHandleValue = new IntPtr(-1);
    Win32FindData _findFileData;
    private IntPtr _hFind = InvalidHandleValue;
    private double MAXDWORD = 4294967295;
    private void FindFileInDir(string root, string pid, List<UpAndDownFolderInfo> lis)
    {
    _mScopes.Clear();
    KeyValuePair<string, string> kvp = new KeyValuePair<string, string>(root, pid);
    start:
    string path = kvp.Key;
    string parentId = kvp.Value;
    new FileIOPermission(FileIOPermissionAccess.PathDiscovery, Path.Combine(path, ".")).Demand();
    if (path[path.Length - 1] != '\')
    {
    path = path + "\";
    }
    _hFind = FindFirstFile(Path.Combine(path, "*"), ref _findFileData);

    string guid = Guid.NewGuid().ToString();
    UpAndDownFolderInfo udfolder = new UpAndDownFolderInfo
    {
    CreateDate = DateTime.Now,
    LGuid = guid,
    Name = path.TrimEnd('\').Substring(path.TrimEnd('\').LastIndexOf('\') + 1),
    CreaterGuid = CurRuningData.LoginUser.WGuid,
    ParentGuid = parentId,
    TransState = (byte)EUpAndDownFileStateType.正在传输,
    WGuid = guid,
    WorkType = (byte)EUpAndDownFileWorkType.上传
    };
    do
    {
    if (_hFind != InvalidHandleValue)
    {
    //将本目录和上一层目录过滤掉
    if (_findFileData.cFileName.Equals(@".") || _findFileData.cFileName.Equals(@"..") || _findFileData.dwFileAttributes == (int)FileAttributes.Hidden)
    continue;
    //如果是文件夹
    if ((_findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY &&
    (_findFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != FILE_ATTRIBUTE_HIDDEN)
    {
    _mScopes.Push(new KeyValuePair<string, string>(Path.Combine(path, _findFileData.cFileName), udfolder.LGuid));
    }
    //反之则是文件
    else
    {
    string ft = _findFileData.cFileName.Substring(_findFileData.cFileName.LastIndexOf('.') + 1);
    if (ft != "wjyishandsomeboy" && (_findFileData.dwFileAttributes & FILE_ATTRIBUTE_HIDDEN) != FILE_ATTRIBUTE_HIDDEN)
    {
    UpAndDownFileInfo udfile = new UpAndDownFileInfo
    {
    CreateDate = DateTime.Now,
    CreaterGuid = CurRuningData.LoginUser.WGuid,
    FilePath = path + _findFileData.cFileName,
    FileType = ft,
    LGuid = Guid.NewGuid().ToString(),
    Name = _findFileData.cFileName,//.Substring(0, _findFileData.cFileName.LastIndexOf('.') - 1),
    ParentGuid = udfolder.LGuid,
    TransState = (byte)EUpAndDownFileStateType.等待传输,
    UsedSpaceSizeB = (long)(_findFileData.nFileSizeHigh * (MAXDWORD + 1) + _findFileData.nFileSizeLow),
    WGuid = Guid.Empty.ToString(),
    WorkType = (byte)EUpAndDownFileWorkType.上传,
    MD5 = ""//FileHelper.GetMD5(path + _findFileData.cFileName)
    };
    udfolder.ChildFiles.Add(udfile);
    }
    }
    }
    }
    while (FindNextFile(_hFind, ref _findFileData));
    lis.Add(udfolder);
    FindClose(_hFind);
    if (_mScopes.Count > 0)
    {
    kvp = _mScopes.Pop();
    goto start;
    }
    }

    #endregion

  • 相关阅读:
    字符串常量
    二维数组中的查找
    Codeforces 156B Suspects——————【逻辑判断】
    Codeforces 156 A——Message——————【思维题】
    Codeforces 639B——Bear and Forgotten Tree 3——————【构造、树】
    Codeforces 671 A——Recycling Bottles——————【思维题】
    Codeforces 550D —— Regular Bridge——————【构造】
    Codeforces 550C —— Divisibility by Eight——————【枚举 || dp】
    codeforces 638B—— Making Genome in Berland——————【类似拓扑排序】
    codeforces 675 C ——Money Transfers——————【思维题】
  • 原文地址:https://www.cnblogs.com/yt954437595/p/5362343.html
Copyright © 2011-2022 走看看