• C# FindFirstFile win32API (转)


      private void button2_Click(object sender, EventArgs e)
            {
                WIN32_FIND_DATA FindFileData1 = new WIN32_FIND_DATA();
                var file = "C:\Windows\System32\osk.exe";
                //以32位方式编译 找不到文件 。。。  
                var hFind = FindFirstFile(file, ref FindFileData1);
    
    
            }
    
            #region 声明WIN32API函数以及结构 **************************************
            [Serializable,
            System.Runtime.InteropServices.StructLayout
              (System.Runtime.InteropServices.LayoutKind.Sequential,
              CharSet = System.Runtime.InteropServices.CharSet.Auto
              ),
            System.Runtime.InteropServices.BestFitMapping(false)]
            private struct WIN32_FIND_DATA
            {
                public int dwFileAttributes;
                public int ftCreationTime_dwLowDateTime;
                public int ftCreationTime_dwHighDateTime;
                public int ftLastAccessTime_dwLowDateTime;
                public int ftLastAccessTime_dwHighDateTime;
                public int ftLastWriteTime_dwLowDateTime;
                public int ftLastWriteTime_dwHighDateTime;
                public int nFileSizeHigh;
                public int nFileSizeLow;
                public int dwReserved0;
                public int dwReserved1;
                [System.Runtime.InteropServices.MarshalAs
                  (System.Runtime.InteropServices.UnmanagedType.ByValTStr,
                  SizeConst = 260)]
                public string cFileName;
                [System.Runtime.InteropServices.MarshalAs
                  (System.Runtime.InteropServices.UnmanagedType.ByValTStr,
                  SizeConst = 14)]
                public string cAlternateFileName;
            }
            [System.Runtime.InteropServices.DllImport
              ("kernel32.dll",
              CharSet = System.Runtime.InteropServices.CharSet.Auto,
              SetLastError = true)]
            private static extern IntPtr FindFirstFile(string pFileName, ref WIN32_FIND_DATA pFindFileData);
            [System.Runtime.InteropServices.DllImport
              ("kernel32.dll",
              CharSet = System.Runtime.InteropServices.CharSet.Auto,
              SetLastError = true)]
            private static extern bool FindNextFile(IntPtr hndFindFile, ref WIN32_FIND_DATA lpFindFileData);
            [System.Runtime.InteropServices.DllImport("kernel32.dll", SetLastError = true)]
            private static extern bool FindClose(IntPtr hndFindFile);
            #endregion
    
            //具体方法函数
    
            Stack<string> m_scopes = new Stack<string>();
            private static readonly IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1);
            WIN32_FIND_DATA FindFileData;
            private System.IntPtr hFind = INVALID_HANDLE_VALUE;
            void FindFileInDir(string rootDir)
            {
                string path = rootDir;
                start:
                new FileIOPermission(FileIOPermissionAccess.PathDiscovery, Path.Combine(path, ".")).Demand();
                if (path[path.Length - 1] != '\')
                {
                    path = path + "\";
                }
                //Response.Write("文件夹为:" + path + "<br>");
                hFind = FindFirstFile(Path.Combine(path, "*"), ref FindFileData);
                if (hFind != INVALID_HANDLE_VALUE)
                {
                    do
                    {
                        if (FindFileData.cFileName.Equals(@".") || FindFileData.cFileName.Equals(@".."))
                            continue;
                        if ((FindFileData.dwFileAttributes & 0x10) != 0)
                        {
                            m_scopes.Push(Path.Combine(path, FindFileData.cFileName));
                        }
                        else
                        {
                            //Response.Write(FindFileData.cFileName + "<br>");
                        }
                    }
                    while (FindNextFile(hFind, ref FindFileData));
                }
                FindClose(hFind);
                if (m_scopes.Count > 0)
                {
                    path = m_scopes.Pop();
                    goto start;
                }
            }

    转自 https://www.cnblogs.com/dekevin/p/3926608.html

  • 相关阅读:
    [转]UNI-APP开发笔记之使用uni.navigateBack修改上一个页面值
    [转]移动端人员选择的设计思考
    [转]nginx安装及其配置详细教程
    [转]Vue 使用use、prototype自定义自己的全局组件
    [转]uniapp项目运行支付宝小程序,报错:xxx.json中没有申明component: true
    支付宝(钉钉)小程序使用uView控制台报错Cannot read property 'title-all' of undefined
    [转]commonJS规范和require,import区别
    [转]module.exports和export详解
    [转]如何在组件中去使用vuex的值和方法?
    [转]CryptoJS中AES256(CBC)加密算法简单使用
  • 原文地址:https://www.cnblogs.com/enych/p/12177128.html
Copyright © 2020-2023  润新知