public static DateTime GetFileCreationTime( string filePath) { filePath = CheckAddLongPathPrefix(filePath); PInvokeHelper.WIN32_FIND_DATA fd; var result = PInvokeHelper.FindFirstFile(filePath.TrimEnd('\\'), out fd); if (result == PInvokeHelper.INVALID_HANDLE_VALUE) { return(DateTime.MinValue); } try { if (result.ToInt64() == PInvokeHelper.ERROR_FILE_NOT_FOUND) { return(DateTime.MinValue); } else { var ft = fd.ftCreationTime; var hft2 = (((long)ft.dwHighDateTime) << 32) + ft.dwLowDateTime; return(getLocalTime(hft2)); } } finally { PInvokeHelper.FindClose(result); } }
public static ZlpFileInfo[] GetFiles(string directoryPath, string pattern, SearchOption searchOption) { directoryPath = CheckAddLongPathPrefix(directoryPath); var results = new List <ZlpFileInfo>(); PInvokeHelper.WIN32_FIND_DATA findData; var findHandle = PInvokeHelper.FindFirstFile(directoryPath.TrimEnd('\\') + "\\" + pattern, out findData); if (findHandle != PInvokeHelper.INVALID_HANDLE_VALUE) { try { bool found; do { var currentFileName = findData.cFileName; // if this is a file, find its contents if (((int)findData.dwFileAttributes & PInvokeHelper.FILE_ATTRIBUTE_DIRECTORY) == 0) { results.Add(new ZlpFileInfo(ZlpPathHelper.Combine(directoryPath, currentFileName))); } // find next found = PInvokeHelper.FindNextFile(findHandle, out findData); } while (found); } finally { // close the find handle PInvokeHelper.FindClose(findHandle); } } if (searchOption == SearchOption.AllDirectories) { foreach (var dir in GetDirectories(directoryPath)) { results.AddRange(GetFiles(dir.FullName, pattern, searchOption)); } } return(results.ToArray()); }
public static ZlpDirectoryInfo[] GetDirectories(string directoryPath, string pattern) { directoryPath = CheckAddLongPathPrefix(directoryPath); var results = new List <ZlpDirectoryInfo>(); PInvokeHelper.WIN32_FIND_DATA findData; var findHandle = PInvokeHelper.FindFirstFile(directoryPath.TrimEnd('\\') + @"\" + pattern, out findData); if (findHandle != PInvokeHelper.INVALID_HANDLE_VALUE) { try { bool found; do { var currentFileName = findData.cFileName; // if this is a directory, find its contents if (((int)findData.dwFileAttributes & PInvokeHelper.FILE_ATTRIBUTE_DIRECTORY) != 0) { if (currentFileName != @"." && currentFileName != @"..") { results.Add(new ZlpDirectoryInfo(ZlpPathHelper.Combine(directoryPath, currentFileName))); } } // find next found = PInvokeHelper.FindNextFile(findHandle, out findData); } while (found); } finally { // close the find handle PInvokeHelper.FindClose(findHandle); } } return(results.ToArray()); }
public static long GetFileLength(string filePath) { Trace.TraceInformation(@"About to get file length for path '{0}'.", filePath); // 2014-06-10, Uwe Keim: Weil das auf 64-bit-Windows 8 nicht sauber läuft, // zunächst mal bei kürzeren Pfaden die eingebaute Methode nehmen. if (!MustBeLongPath(filePath)) { return(new FileInfo(filePath).Length); } filePath = CheckAddLongPathPrefix(filePath); PInvokeHelper.WIN32_FIND_DATA fd; var result = PInvokeHelper.FindFirstFile(filePath.TrimEnd('\\'), out fd); if (result == PInvokeHelper.INVALID_HANDLE_VALUE) { return(0); } try { if (result.ToInt64() == PInvokeHelper.ERROR_FILE_NOT_FOUND) { return(0); } else { var sfh = new SafeFileHandle(result, false); if (sfh.IsInvalid) { var num = Marshal.GetLastWin32Error(); if ((num == 2 || num == 3 || num == 21)) // http://msdn.microsoft.com/en-us/library/windows/desktop/ms681382(v=vs.85).aspx { return(0); } else { return(0); } } // http://zetalongpaths.codeplex.com/discussions/580478#post1351470 // https://mcdrummerman.wordpress.com/2010/07/13/win32_find_data-and-negative-file-sizes/ //store nFileSizeLow long fDataFSize = (long)fd.nFileSizeLow; //store individual file size for later accounting usage long fileSize = 0; if (fDataFSize < 0 && (long)fd.nFileSizeHigh > 0) { fileSize = fDataFSize + 0x100000000 + ((long)fd.nFileSizeHigh * 0x100000000); } else { if ((long)fd.nFileSizeHigh > 0) { fileSize = fDataFSize + ((long)fd.nFileSizeHigh * 0x100000000); } else if (fDataFSize < 0) { fileSize = (fDataFSize + 0x100000000); } else { fileSize = fDataFSize; } } return(fileSize); /* * var low = fd.nFileSizeLow; * var high = fd.nFileSizeHigh; * * //return (high * (0xffffffff + 1)) + low; * //return (((ulong)high) << 32) + low; * var l = ((high << 0x20) | (low & 0xffffffffL)); * // Copied from FileInfo.Length via Reflector.NET. * return (ulong) l;*/ var low = fd.nFileSizeLow; var high = fd.nFileSizeHigh; Trace.TraceInformation(@"FindFirstFile returned LOW = {0}, HIGH = {1}.", low, high); Trace.Flush(); try { return((long)high << 32 | ((long)low & (long)(0xffffffffL))); //try //{ //var sign = ((long) high << 32 | (low & 0xffffffffL)); //try //{ //return sign <= 0 ? 0 : unchecked((ulong) sign); //} // catch (OverflowException x) // { // var y = new OverflowException(@"Error getting file length (cast).", x); // y.Data[@"low"] = low; // y.Data[@"high"] = high; // y.Data[@"signed value"] = sign; // throw y; // } //} //catch (OverflowException x) //{ // var y = new OverflowException(@"Error getting file length (sign).", x); // y.Data[@"low"] = low; // y.Data[@"high"] = high; // throw y; //} } catch (OverflowException x) { Trace.TraceInformation( @"Got overflow exception ('{3}') for path '{0}'. LOW = {1}, HIGH = {2}.", filePath, low, high, x.Message); Trace.Flush(); throw; } } } finally { PInvokeHelper.FindClose(result); } }