public static void Move(string source, string destination) { if (!Win32Interop.MoveFileEx( Path.GetLongWin32(source), Path.GetLongWin32(destination), Win32Interop.MoveFileFlags.MOVEFILE_REPLACE_EXISTING | Win32Interop.MoveFileFlags.MOVEFILE_COPY_ALLOWED)) { Win32Interop.ErrorCodes errorCode = (Win32Interop.ErrorCodes)Marshal.GetLastWin32Error(); throw new InvalidOperationException(string.Format("Can't move {0} to {1}, win32api error code {2}", source, destination, errorCode)); } }
internal static void Remove(string path) { string win32Path = Path.GetLongWin32(path); if (!Win32Interop.DeleteFile(win32Path)) { Win32Interop.ErrorCodes errorCode = (Win32Interop.ErrorCodes)Marshal.GetLastWin32Error(); if (errorCode != Win32Interop.ErrorCodes.ERROR_PATH_NOT_FOUND && errorCode != Win32Interop.ErrorCodes.ERROR_FILE_NOT_FOUND) { throw new InvalidOperationException(string.Format("Can't delete file {0}, win32api error code {1}", win32Path, errorCode)); } } }
public static void CreateDirectory(string path) { int currentFolderLastCharNum = 0; string prefix = @"\\"; if (path.IndexOf(prefix) == 0 && path.Length > prefix.Length) { currentFolderLastCharNum = path.IndexOf(System.IO.Path.DirectorySeparatorChar, prefix.Length); } else { prefix = @":\"; if (path.IndexOf(prefix) == 0 && path.Length > prefix.Length) { currentFolderLastCharNum = prefix.Length; } } while (currentFolderLastCharNum > -1 && currentFolderLastCharNum + 1 < path.Length) { currentFolderLastCharNum = path.IndexOf(System.IO.Path.DirectorySeparatorChar, currentFolderLastCharNum + 1); if (currentFolderLastCharNum == -1) { break; } string currentFolder = path.Substring(0, currentFolderLastCharNum); if (!System.IO.Directory.Exists(currentFolder)) { string win32Path = Path.GetLongWin32(currentFolder); bool result = Win32Interop.CreateDirectory(win32Path, IntPtr.Zero); if (!result) { Win32Interop.ErrorCodes errorCode = (Win32Interop.ErrorCodes)Marshal.GetLastWin32Error(); if (errorCode != Win32Interop.ErrorCodes.ERROR_ALREADY_EXISTS) { throw new InvalidOperationException(string.Format("Can't create directory {0}, win32api error code {1}", win32Path, errorCode)); } } } } }
internal static bool Exists(string path) { Win32Interop.FileAttributesFlags result = Win32Interop.GetFileAttributes(Path.GetLongWin32(path)); return(result == Win32Interop.FileAttributesFlags.FILE_ATTRIBUTE_DIRECTORY); }