public static void Move(String sourceFileName, String destFileName) { if (Settings.IsUnix) { System.IO.File.Move(sourceFileName, destFileName); return; } if (sourceFileName == null || destFileName == null) { throw new ArgumentNullException((sourceFileName == null ? "sourceFileName" : "destFileName"), "ArgumentNull_FileName"); } if (sourceFileName.Length == 0 || destFileName.Length == 0) { throw new ArgumentException("Argument_EmptyFileName", (sourceFileName.Length == 0 ? "sourceFileName" : "destFileName")); } String fullSourceFileName = NameFix.AddLongPathPrefix(sourceFileName); String fullDestFileName = NameFix.AddLongPathPrefix(destFileName); if (!Exists(fullSourceFileName)) { throw new Exception("ERROR_FILE_NOT_FOUND" + fullSourceFileName); } if (!Win32Native.MoveFile(fullSourceFileName, fullDestFileName)) { int hr = Marshal.GetLastWin32Error(); throw new Exception("ERROR_MOVING_FILE. Error Code (" + hr + ")"); } }
public static int OpenFileWrite(string path, out System.IO.Stream stream) { if (Settings.IsUnix) { try { stream = new System.IO.FileStream(path, System.IO.FileMode.Create, System.IO.FileAccess.Write); return(0); } catch (Exception) { stream = null; return(Marshal.GetLastWin32Error()); } } string filename = NameFix.AddLongPathPrefix(path); SafeFileHandle hFile = Win32Native.CreateFile(filename, GENERIC_WRITE, System.IO.FileShare.None, IntPtr.Zero, System.IO.FileMode.Create, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (hFile.IsInvalid) { stream = null; return(Marshal.GetLastWin32Error()); } stream = new System.IO.FileStream(hFile, System.IO.FileAccess.Write); return(0); }
public DirectoryInfo(string path) { FullName = path; Name = Path.GetFileName(path); if (Settings.IsUnix) { System.IO.DirectoryInfo fi = new System.IO.DirectoryInfo(path); if (!fi.Exists) { return; } LastWriteTime = fi.LastWriteTimeUtc.Ticks; return; } string fileName = NameFix.AddLongPathPrefix(path); Win32Native.WIN32_FILE_ATTRIBUTE_DATA wIn32FileAttributeData = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA(); bool b = Win32Native.GetFileAttributesEx(fileName, 0, ref wIn32FileAttributeData); if (!b || (wIn32FileAttributeData.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0) { return; } LastWriteTime = Convert.Time(wIn32FileAttributeData.ftLastWriteTimeHigh, wIn32FileAttributeData.ftLastWriteTimeLow); }
public FileInfo[] GetFiles(string SearchPattern, bool includeHidden = true) { List <FileInfo> files = new List <FileInfo>(); if (Settings.IsUnix) { System.IO.DirectoryInfo di = new System.IO.DirectoryInfo(FullName); System.IO.FileInfo[] arrDi = di.GetFiles(SearchPattern); foreach (System.IO.FileInfo tDi in arrDi) { FileInfo lDi = new FileInfo { Name = tDi.Name, FullName = Path.Combine(FullName, tDi.Name), Length = tDi.Length, LastWriteTime = tDi.LastWriteTimeUtc.Ticks }; files.Add(lDi); } return(files.ToArray()); } string dirName = NameFix.AddLongPathPrefix(FullName); Win32Native.WIN32_FIND_DATA findData = new Win32Native.WIN32_FIND_DATA(); SafeFindHandle findHandle = Win32Native.FindFirstFile(dirName + @"\" + SearchPattern, findData); if (!findHandle.IsInvalid) { do { string currentFileName = findData.cFileName; // if this is a directory, find its contents if ((findData.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) != 0) { continue; } if (!includeHidden && (findData.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_HIDDEN) != 0) { continue; } FileInfo fi = new FileInfo { Name = currentFileName, FullName = Path.Combine(FullName, currentFileName), Length = Convert.Length(findData.nFileSizeHigh, findData.nFileSizeLow), LastWriteTime = Convert.Time(findData.ftLastWriteTimeHigh, findData.ftLastWriteTimeLow) }; files.Add(fi); }while (Win32Native.FindNextFile(findHandle, findData)); } // close the find handle findHandle.Dispose(); return(files.ToArray()); }
public static bool Exists(string path) { string fixPath = NameFix.AddLongPathPrefix(path); Win32Native.WIN32_FILE_ATTRIBUTE_DATA wIn32FileAttributeData = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA(); bool b = Win32Native.GetFileAttributesEx(fixPath, 0, ref wIn32FileAttributeData); return(b && (wIn32FileAttributeData.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0); }
public static void Delete(String path) { if (Settings.IsUnix) { System.IO.Directory.Delete(path); return; } String fullPath = NameFix.AddLongPathPrefix(path); Win32Native.RemoveDirectory(fullPath); }
public static void Copy(String sourceFileName, String destFileName, bool overwrite) { if (Settings.IsUnix) { System.IO.File.Copy(sourceFileName, destFileName, overwrite); return; } if (sourceFileName == null || destFileName == null) { throw new ArgumentNullException((sourceFileName == null ? "sourceFileName" : "destFileName"), "ArgumentNull_FileName"); } if (sourceFileName.Length == 0 || destFileName.Length == 0) { throw new ArgumentException("Argument_EmptyFileName", (sourceFileName.Length == 0 ? "sourceFileName" : "destFileName")); } String fullSourceFileName = NameFix.AddLongPathPrefix(sourceFileName); String fullDestFileName = NameFix.AddLongPathPrefix(destFileName); bool r = Win32Native.CopyFile(fullSourceFileName, fullDestFileName, !overwrite); if (!r) { // Save Win32 error because subsequent checks will overwrite this HRESULT. int errorCode = Marshal.GetLastWin32Error(); String fileName = destFileName; /* * if (errorCode != Win32Native.ERROR_FILE_EXISTS) * { * // For a number of error codes (sharing violation, path * // not found, etc) we don't know if the problem was with * // the source or dest file. Try reading the source file. * using (SafeFileHandle handle = Win32Native.UnsafeCreateFile(fullSourceFileName, FileStream.GENERIC_READ, FileShare.Read, null, FileMode.Open, 0, IntPtr.Zero)) * { * if (handle.IsInvalid) * fileName = sourceFileName; * } * * if (errorCode == Win32Native.ERROR_ACCESS_DENIED) * { * if (Directory.InternalExists(fullDestFileName)) * throw new IOException(String.Format(CultureInfo.CurrentCulture, Environment.GetResourceString("Arg_FileIsDirectory_Name"), destFileName), Win32Native.ERROR_ACCESS_DENIED, fullDestFileName); * } * } * * __Error.WinIOError(errorCode, fileName); * */ } }
public static void Delete(String path) { string fixPath = NameFix.AddLongPathPrefix(path); if (!Win32Native.DeleteFile(fixPath)) { int hr = Marshal.GetLastWin32Error(); if (hr != Win32Native.ERROR_FILE_NOT_FOUND) { throw new Exception("Error while deleting file :" + path); } } }
public static void CreateDirectory(String path) { if (path == null) { throw new ArgumentNullException("path"); } if (path.Length == 0) { throw new ArgumentException("Argument_PathEmpty"); } String fullPath = NameFix.AddLongPathPrefix(path); Win32Native.CreateDirectory(fullPath, IntPtr.Zero); }
public static bool Exists(string path) { if (Settings.IsUnix) { return(System.IO.Directory.Exists(path)); } string fixPath = NameFix.AddLongPathPrefix(path); Win32Native.WIN32_FILE_ATTRIBUTE_DATA wIn32FileAttributeData = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA(); bool b = Win32Native.GetFileAttributesEx(fixPath, 0, ref wIn32FileAttributeData); return(b && (wIn32FileAttributeData.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) != 0); }
public DirectoryInfo[] GetDirectories(string SearchPattern, bool includeHidden = true) { List <DirectoryInfo> dirs = new List <DirectoryInfo>(); string dirName = NameFix.AddLongPathPrefix(FullName); Win32Native.WIN32_FIND_DATA findData = new Win32Native.WIN32_FIND_DATA(); SafeFindHandle findHandle = Win32Native.FindFirstFile(dirName + @"\" + SearchPattern, findData); if (!findHandle.IsInvalid) { do { string currentFileName = findData.cFileName; // if this is a directory, find its contents if ((findData.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0) { continue; } if (currentFileName == "." || currentFileName == "..") { continue; } if (!includeHidden && (findData.dwFileAttributes & Win32Native.FILE_ATTRIBUTE_HIDDEN) != 0) { continue; } DirectoryInfo di = new DirectoryInfo { Name = currentFileName, FullName = Path.Combine(FullName, currentFileName), LastWriteTime = Convert.Time(findData.ftLastWriteTimeHigh, findData.ftLastWriteTimeLow), fileAttributes = findData.dwFileAttributes }; dirs.Add(di); }while (Win32Native.FindNextFile(findHandle, findData)); } // close the find handle findHandle.Dispose(); return(dirs.ToArray()); }
public DirectoryInfo(string path) { FullName = path; Name = Path.GetFileName(path); string fileName = NameFix.AddLongPathPrefix(path); Win32Native.WIN32_FILE_ATTRIBUTE_DATA wIn32FileAttributeData = new Win32Native.WIN32_FILE_ATTRIBUTE_DATA(); bool b = Win32Native.GetFileAttributesEx(fileName, 0, ref wIn32FileAttributeData); if (!b || (wIn32FileAttributeData.fileAttributes & Win32Native.FILE_ATTRIBUTE_DIRECTORY) == 0) { return; } LastWriteTime = Convert.Time(wIn32FileAttributeData.ftLastWriteTimeHigh, wIn32FileAttributeData.ftLastWriteTimeLow); fileAttributes = wIn32FileAttributeData.fileAttributes; }
public static void Move(String sourceDirName, String destDirName) { if (Settings.IsUnix) { System.IO.Directory.Move(sourceDirName, destDirName); return; } if (sourceDirName == null) { throw new ArgumentNullException("sourceDirName"); } if (sourceDirName.Length == 0) { throw new ArgumentException("Argument_EmptyFileName", "sourceDirName"); } if (destDirName == null) { throw new ArgumentNullException("destDirName"); } if (destDirName.Length == 0) { throw new ArgumentException("Argument_EmptyFileName", "destDirName"); } String fullsourceDirName = NameFix.AddLongPathPrefix(sourceDirName); String fulldestDirName = NameFix.AddLongPathPrefix(destDirName); if (!Win32Native.MoveFile(fullsourceDirName, fulldestDirName)) { int hr = Marshal.GetLastWin32Error(); if (hr == Win32Native.ERROR_FILE_NOT_FOUND) // Source dir not found { throw new Exception("ERROR_PATH_NOT_FOUND " + fullsourceDirName); } if (hr == Win32Native.ERROR_ACCESS_DENIED) // WinNT throws IOException. This check is for Win9x. We can't change it for backcomp. { throw new Exception("UnauthorizedAccess_IODenied_Path" + sourceDirName); } } }
// errorMessage = new Win32Exception(errorCode).Message; public static int OpenFileRead(string path, out System.IO.Stream stream) { string filename = NameFix.AddLongPathPrefix(path); SafeFileHandle hFile = Win32Native.CreateFile(filename, GENERIC_READ, System.IO.FileShare.Read, IntPtr.Zero, System.IO.FileMode.Open, FILE_ATTRIBUTE_NORMAL, IntPtr.Zero); if (hFile.IsInvalid) { stream = null; return(Marshal.GetLastWin32Error()); } stream = new System.IO.FileStream(hFile, System.IO.FileAccess.Read); return(0); }
public static bool SetAttributes(String path, FileAttributes fileAttributes) { if (Settings.IsUnix) { try { System.IO.File.SetAttributes(path, (System.IO.FileAttributes)fileAttributes); return(true); } catch (Exception) { return(false); } } String fullPath = NameFix.AddLongPathPrefix(path); return(Win32Native.SetFileAttributes(fullPath, (int)fileAttributes)); }
public static void Delete(String path) { if (Program.rvSettings.IsUnix) { System.IO.File.Delete(path); return; } string fixPath = NameFix.AddLongPathPrefix(path); if (!Win32Native.DeleteFile(fixPath)) { int hr = Marshal.GetLastWin32Error(); if (hr != Win32Native.ERROR_FILE_NOT_FOUND) { throw new Exception("Error while deleting file :" + path); } } }
public static void CreateDirectory(String path) { if (Program.rvSettings.IsUnix) { System.IO.Directory.CreateDirectory(path); return; } if (path == null) { throw new ArgumentNullException("path"); } if (path.Length == 0) { throw new ArgumentException("Argument_PathEmpty"); } String fullPath = NameFix.AddLongPathPrefix(path); Win32Native.CreateDirectory(fullPath, IntPtr.Zero); }
public static void Delete(String path) { String fullPath = NameFix.AddLongPathPrefix(path); Win32Native.RemoveDirectory(fullPath); }
public static bool SetAttributes(String path, FileAttributes fileAttributes) { String fullPath = NameFix.AddLongPathPrefix(path); return(Win32Native.SetFileAttributes(fullPath, (int)fileAttributes)); }