/* * DeleteDirectory - Deletes a directory and it's contents from a local or * remote file system (via recusive delete file/directory). */ public static WinError DeleteDirectory( string lpDirectoryPath ) { WinError dwError = WinError.NO_ERROR; IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); IntPtr handle = INVALID_HANDLE_VALUE; Likewise.LMC.FileClient.InteropWindows.WIN32_FIND_DATA WinFindFileData = new Likewise.LMC.FileClient.InteropWindows.WIN32_FIND_DATA(); Likewise.LMC.FileClient.InteropLikewise.LIKEWISE_FIND_DATA LwFindFileData = new Likewise.LMC.FileClient.InteropLikewise.LIKEWISE_FIND_DATA(); string PathSeparator = useWindowsDlls ? "\\" : "/"; bool success = false; string search = lpDirectoryPath + PathSeparator + "*"; if (useWindowsDlls) { handle = InteropWindows.FindFirstFileW(search, ref WinFindFileData); } else { handle = InteropLikewise.FindFirstFile(search, ref LwFindFileData); } if (handle != INVALID_HANDLE_VALUE) { success = true; } else { goto cleanup; } while (success) { FileItem file = new FileItem(); if (useWindowsDlls) { file.FileName = WinFindFileData.cFileName; file.Alternate = WinFindFileData.cAlternate; } else { file.FileName = LwFindFileData.FileName; file.Alternate = LwFindFileData.Alternate; } // Skip to next item, dont need to move these if (String.Compare(file.FileName, ".") == 0 || String.Compare(file.FileName, "..") == 0) { if (useWindowsDlls) { success = InteropWindows.FindNextFileW(handle, ref WinFindFileData); } else { success = InteropLikewise.FindNextFile(handle, ref LwFindFileData); } continue; } // Determine file type if (useWindowsDlls) { if ((WinFindFileData.dwFileAttributes & FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) { file.IsDirectory = true; } } else { if ((LwFindFileData.dwFileAttributes & FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) { file.IsDirectory = true; } } // Delete current item if (file.IsDirectory) { dwError = DeleteDirectory(lpDirectoryPath + PathSeparator + file.FileName); if (dwError != WinError.NO_ERROR) { goto error; } } else { dwError = DeleteFile(lpDirectoryPath + PathSeparator + file.FileName); if (dwError != WinError.NO_ERROR) { goto error; } } // Get next item if (useWindowsDlls) { success = InteropWindows.FindNextFileW(handle, ref WinFindFileData); } else { success = InteropLikewise.FindNextFile(handle, ref LwFindFileData); } } if (useWindowsDlls) { success = InteropWindows.FindClose(handle); } else { success = InteropLikewise.FindClose(handle); } handle = INVALID_HANDLE_VALUE; dwError = RemoveDirectory(lpDirectoryPath); if (dwError != WinError.NO_ERROR) { goto error; } cleanup: if (handle != INVALID_HANDLE_VALUE) { if (useWindowsDlls) { success = InteropWindows.FindClose(handle); } else { success = InteropLikewise.FindClose(handle); } } return(dwError); error: goto cleanup; }
/* * MoveDirectory - Moves the full file directory contents to and from a local or * remote file system (via recursive create folder/copy file/delete original). */ public static WinError MoveDirectory( string lpSourceParentPath, string lpDestParentPath, string lpSourceDirectoryName, string lpDestDirectoryName, bool moveHiddenFiles ) { WinError dwError = WinError.NO_ERROR; IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); IntPtr handle = INVALID_HANDLE_VALUE; Likewise.LMC.FileClient.InteropWindows.WIN32_FIND_DATA WinFindFileData = new Likewise.LMC.FileClient.InteropWindows.WIN32_FIND_DATA(); Likewise.LMC.FileClient.InteropLikewise.LIKEWISE_FIND_DATA LwFindFileData = new Likewise.LMC.FileClient.InteropLikewise.LIKEWISE_FIND_DATA(); string PathSeparator = useWindowsDlls ? "\\" : "/"; bool success = false; string sourceDir = lpSourceParentPath + PathSeparator + lpSourceDirectoryName; string destDir = lpDestParentPath + PathSeparator + lpDestDirectoryName; string search = sourceDir + PathSeparator + "*"; // Create the destination directory dwError = CreateDirectory(destDir); if (dwError != WinError.NO_ERROR) { goto error; } if (useWindowsDlls) { handle = InteropWindows.FindFirstFileW(search, ref WinFindFileData); } else { handle = InteropLikewise.FindFirstFile(search, ref LwFindFileData); } if (handle != INVALID_HANDLE_VALUE) { success = true; } while (success) { FileItem file = new FileItem(); if (useWindowsDlls) { file.FileName = WinFindFileData.cFileName; file.Alternate = WinFindFileData.cAlternate; } else { file.FileName = LwFindFileData.FileName; file.Alternate = LwFindFileData.Alternate; } // Skip to next item, dont need to move these if (String.Compare(file.FileName, ".") == 0 || String.Compare(file.FileName, "..") == 0) { if (useWindowsDlls) { success = InteropWindows.FindNextFileW(handle, ref WinFindFileData); } else { success = InteropLikewise.FindNextFile(handle, ref LwFindFileData); } continue; } // Skip to next item if we dont want hidden items if (!moveHiddenFiles && file.FileName[0] == '.') { if (useWindowsDlls) { success = InteropWindows.FindNextFileW(handle, ref WinFindFileData); } else { success = InteropLikewise.FindNextFile(handle, ref LwFindFileData); } continue; } // Determine file type if (useWindowsDlls) { if ((WinFindFileData.dwFileAttributes & FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) { file.IsDirectory = true; } } else { if ((LwFindFileData.dwFileAttributes & FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) { file.IsDirectory = true; } } // Move current item to new folder if (file.IsDirectory) { dwError = MoveDirectory(sourceDir, destDir, file.FileName, file.FileName, moveHiddenFiles); if (dwError != WinError.NO_ERROR) { goto error; } } else { string sourceFile = sourceDir + PathSeparator + file.FileName; string destFile = destDir + PathSeparator + file.FileName; dwError = MoveFile(sourceFile, destFile); if (dwError != WinError.NO_ERROR) { goto error; } } // Get next item if (useWindowsDlls) { success = InteropWindows.FindNextFileW(handle, ref WinFindFileData); } else { success = InteropLikewise.FindNextFile(handle, ref LwFindFileData); } } if (useWindowsDlls) { success = InteropWindows.FindClose(handle); } else { success = InteropLikewise.FindClose(handle); } handle = INVALID_HANDLE_VALUE; dwError = RemoveDirectory(sourceDir); if (dwError != WinError.NO_ERROR) { goto error; } cleanup: if (handle != INVALID_HANDLE_VALUE) { if (useWindowsDlls) { success = InteropWindows.FindClose(handle); } else { success = InteropLikewise.FindClose(handle); } } return(dwError); error: goto cleanup; }