/* * MoveFile - Move a file to/from a local or remote file system. */ public static WinError MoveFile( string lpExistingFileName, string lpNewFileName ) { bool moved = false; WinError error = 0; if (useWindowsDlls) { moved = InteropWindows.MoveFile(lpExistingFileName, lpNewFileName); if (!moved) { error = (WinError)Marshal.GetLastWin32Error(); } } else { moved = InteropLikewise.MoveFile(lpExistingFileName, lpNewFileName); if (!moved) { error = (WinError)InteropLikewise.GetLastError(); } } return(error); }
/* * DeleteFile - Deletes a file from a local or remote file system. */ public static WinError DeleteFile( string lpFileName ) { bool deleted = false; WinError error = 0; if (useWindowsDlls) { deleted = InteropWindows.DeleteFile(lpFileName); if (!deleted) { error = (WinError)Marshal.GetLastWin32Error(); } } else { deleted = InteropLikewise.DeleteFile(lpFileName); if (!deleted) { error = (WinError)InteropLikewise.GetLastError(); } } return(error); }
/* * CopyFile - Copy a file to/from a local or remote file system. */ public static WinError CopyFile( string lpExistingFileName, string lpNewFileName, bool bFailIfExists ) { bool copied = false; WinError error = 0; if (useWindowsDlls) { copied = InteropWindows.CopyFile(lpExistingFileName, lpNewFileName, bFailIfExists); if (!copied) { error = (WinError)Marshal.GetLastWin32Error(); } } else { copied = InteropLikewise.CopyFile(lpExistingFileName, lpNewFileName, bFailIfExists); if (!copied) { error = (WinError)InteropLikewise.GetLastError(); } } return(error); }
/* * CreateDirectory - Creates a new directory file on a local or remote file system. */ public static WinError CreateDirectory( string lpDirectoryName ) { bool created = false; WinError error = 0; IntPtr securityDescriptor = new IntPtr(0); if (useWindowsDlls) { created = InteropWindows.CreateDirectory(lpDirectoryName, securityDescriptor); if (!created) { error = (WinError)Marshal.GetLastWin32Error(); } } else { created = InteropLikewise.CreateDirectory(lpDirectoryName); if (!created) { error = (WinError)InteropLikewise.GetLastError(); } } return(error); }
/* * RemoveDirectory - Removes an empty directory file from a local or remote file system. */ public static WinError RemoveDirectory( string lpDirectoryName ) { bool deleted = false; WinError error = 0; if (useWindowsDlls) { deleted = InteropWindows.RemoveDirectory(lpDirectoryName); if (!deleted) { error = (WinError)Marshal.GetLastWin32Error(); } } else { deleted = InteropLikewise.RemoveDirectory(lpDirectoryName); if (!deleted) { error = (WinError)InteropLikewise.GetLastError(); } } return(error); }
/* * GetFileSecurity - Get the file security descriptor object */ public static WinError GetFileSecurity( string lpFilename ) { WinError Winerror = WinError.ERROR_SUCCESS; IntPtr pSecurityDescriptor = new IntPtr(0); uint lpnLengthNeeded = 0; if (useWindowsDlls) { Winerror = InteropWindows.GetFileSecurity(lpFilename, SecurityDescriptorApi.SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SecurityDescriptorApi.SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION | SecurityDescriptorApi.SECURITY_INFORMATION.DACL_SECURITY_INFORMATION, ref pSecurityDescriptor, 0, out lpnLengthNeeded); if ((int)Winerror == 122) //Insufficient buffer { Winerror = InteropWindows.GetFileSecurity(lpFilename, SecurityDescriptorApi.SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION | SecurityDescriptorApi.SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION | SecurityDescriptorApi.SECURITY_INFORMATION.DACL_SECURITY_INFORMATION, ref pSecurityDescriptor, lpnLengthNeeded, out lpnLengthNeeded); SecurityDescriptor.objectType = System.IO.File.Exists(lpFilename) ? SecurityDescriptorApi.SE_OBJECT_TYPE.SE_FILE_OBJECT : SecurityDescriptorApi.SE_OBJECT_TYPE.SE_DIR_OBJECT; if (Winerror != 0) { goto error; } } } else { Winerror = WinError.ERROR_INVALID_PARAMETER; } if (Winerror == WinError.ERROR_EXTENDED_ERROR) { Winerror = (WinError)Marshal.GetLastWin32Error(); } cleanup: if (pSecurityDescriptor != IntPtr.Zero) { SecurityDescriptorApi.CloseHandle(pSecurityDescriptor); } return(Winerror); error: goto cleanup; }
public static WinError SetFileSecurity( string lpFilename, IntPtr pSecurityDescriptor) { WinError Winerror = WinError.ERROR_SUCCESS; if (useWindowsDlls) { Winerror = InteropWindows.SetFileSecurity( lpFilename, SecurityDescriptorApi.SECURITY_INFORMATION.DACL_SECURITY_INFORMATION | SecurityDescriptorApi.SECURITY_INFORMATION.GROUP_SECURITY_INFORMATION | SecurityDescriptorApi.SECURITY_INFORMATION.OWNER_SECURITY_INFORMATION, pSecurityDescriptor); if (Winerror != 0) { goto cleanup; } } error: return(Winerror); cleanup: goto error; }
/* * BeginEnumNetResources - Begin search for connections. Options to find connected, connectable, remembered locations. */ public static WinError BeginEnumNetResources( ResourceScope dwScope, ResourceType dwType, ResourceUsage dwUsage, NETRESOURCE pNetResource, out IntPtr enumHandle ) { WinError error = WinError.ERROR_SUCCESS; if (useWindowsDlls) { error = InteropWindows.WNetOpenEnum(dwScope, dwType, dwUsage, pNetResource, out enumHandle); } else { error = WinError.ERROR_INVALID_PARAMETER; enumHandle = new IntPtr(0); } if (error == WinError.ERROR_EXTENDED_ERROR) { error = (WinError)Marshal.GetLastWin32Error(); } return(error); }
/* * CreateConnection - Adds a connection to a network SMB file share. Optional credentials * can be passed, otherwise default will be used for connection. */ public static WinError CreateConnection( string networkName, string username, string password ) { WinError error = WinError.ERROR_SUCCESS; NETRESOURCE netResource = new NETRESOURCE(); netResource.dwScope = ResourceScope.RESOURCE_GLOBALNET; netResource.dwType = ResourceType.RESOURCETYPE_DISK; netResource.dwDisplayType = ResourceDisplayType.RESOURCEDISPLAYTYPE_SHARE; netResource.dwUsage = ResourceUsage.RESOURCEUSAGE_ALL; netResource.pRemoteName = networkName; if (useWindowsDlls) { error = InteropWindows.WNetAddConnection2(netResource, password, username, 0); } else { error = InteropLikewise.WNetAddConnection2(netResource, password, username, 0); } return(error); }
/* * EnumNetResources - Continue search for connections. */ public static WinError EnumNetResources( IntPtr enumHandle, out List <NETRESOURCE> NetResources ) { WinError error = WinError.ERROR_SUCCESS; List <NETRESOURCE> nrList = new List <NETRESOURCE>(); int cEntries = -1; int bufferSize = 16384; IntPtr ptrBuffer = Marshal.AllocHGlobal(bufferSize); NETRESOURCE nr; for (; ;) { cEntries = -1; bufferSize = 16384; if (useWindowsDlls) { error = InteropWindows.WNetEnumResource(enumHandle, ref cEntries, ptrBuffer, ref bufferSize); } else { error = WinError.ERROR_INVALID_PARAMETER; } if ((error != WinError.NO_ERROR) || (cEntries < 1)) { if (error == WinError.ERROR_NO_MORE_ITEMS) { error = WinError.NO_ERROR; } break; } Int32 ptr = ptrBuffer.ToInt32(); for (int i = 0; i < cEntries; i++) { nr = (NETRESOURCE)Marshal.PtrToStructure(new IntPtr(ptr), typeof(NETRESOURCE)); nrList.Add(nr); ptr += Marshal.SizeOf(nr); } } Marshal.FreeHGlobal(ptrBuffer); NetResources = nrList; return(error); }
/* * DeleteConnection - Cancel a connection to a network SMB file share. */ public static WinError DeleteConnection( string networkName ) { WinError error = WinError.ERROR_SUCCESS; if (useWindowsDlls) { error = InteropWindows.WNetCancelConnection2(networkName, 0, true); } else { error = InteropLikewise.WNetCancelConnection2(networkName, 0, true); } return(error); }
/* * EndEnumNetResources - End search for connections. */ public static WinError EndEnumNetResources( IntPtr enumHandle ) { WinError error = WinError.ERROR_SUCCESS; if (useWindowsDlls) { error = InteropWindows.WNetCloseEnum(enumHandle); } else { error = WinError.ERROR_INVALID_PARAMETER; } if (error == WinError.ERROR_EXTENDED_ERROR) { error = (WinError)Marshal.GetLastWin32Error(); } return(error); }
/* * EnumFiles - Lists all files and directories under a given path location for a local or * remote file system (one level). FileItem list returned details each file type, name, * size, created/modified/accessed times. */ public static List <FileItem> EnumFiles( string filepath, bool showHiddenFiles ) { List <FileItem> Files = new List <FileItem>(); IntPtr INVALID_HANDLE_VALUE = new IntPtr(-1); 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(); bool success = false; string search = null; UInt64 size = 0; UInt64 extra = 0; try { if (useWindowsDlls) { search = filepath + "\\*"; } else { search = filepath + "/*"; } IntPtr handle = INVALID_HANDLE_VALUE; 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; } 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; } if (!showHiddenFiles && file.FileName[0] == '.') { if (useWindowsDlls) { success = InteropWindows.FindNextFileW(handle, ref WinFindFileData); } else { success = InteropLikewise.FindNextFile(handle, ref LwFindFileData); } continue; } if (useWindowsDlls) { size = ((UInt64)WinFindFileData.nFileSizeLow + (UInt64)WinFindFileData.nFileSizeHigh * 4294967296) / 1024; extra = ((UInt64)WinFindFileData.nFileSizeLow + (UInt64)WinFindFileData.nFileSizeHigh * 4294967296) % 1024; if ((WinFindFileData.dwFileAttributes & FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) { file.IsDirectory = true; } } else { size = ((UInt64)LwFindFileData.nFileSizeLow + (UInt64)LwFindFileData.nFileSizeHigh * 4294967296) / 1024; extra = ((UInt64)LwFindFileData.nFileSizeLow + (UInt64)LwFindFileData.nFileSizeHigh * 4294967296) % 1024; if ((LwFindFileData.dwFileAttributes & FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE.FILE_ATTRIBUTE_DIRECTORY) { file.IsDirectory = true; } } SYSTEMTIME created = new SYSTEMTIME(); SYSTEMTIME modified = new SYSTEMTIME(); SYSTEMTIME accessed = new SYSTEMTIME(); DateTime Created = new DateTime(); DateTime Modified = new DateTime(); DateTime Accessed = new DateTime(); if (size != 0 && extra != 0) { size++; } if (useWindowsDlls) { if (WinFindFileData.ftCreationTime.dwHighDateTime != 0 && WinFindFileData.ftCreationTime.dwLowDateTime != 0) { InteropWindows.FileTimeToSystemTime(ref WinFindFileData.ftCreationTime, ref created); } } else { if (LwFindFileData.time_tCreationTime != 0) { InteropLikewise.FileTimeToSystemTime(ref LwFindFileData.time_tCreationTime, ref created); } } Created = new DateTime(created.wYear, created.wMonth, created.wDay, created.wHour, created.wMinute, created.wSecond).ToLocalTime(); if (useWindowsDlls) { if (WinFindFileData.ftLastWriteTime.dwHighDateTime != 0 && WinFindFileData.ftLastWriteTime.dwLowDateTime != 0) { InteropWindows.FileTimeToSystemTime(ref WinFindFileData.ftLastWriteTime, ref modified); } } else { if (LwFindFileData.time_tLastWriteTime != 0) { InteropLikewise.FileTimeToSystemTime(ref LwFindFileData.time_tLastWriteTime, ref modified); } } Modified = new DateTime(modified.wYear, modified.wMonth, modified.wDay, modified.wHour, modified.wMinute, modified.wSecond).ToLocalTime(); if (useWindowsDlls) { if (WinFindFileData.ftLastAccessTime.dwHighDateTime != 0 && WinFindFileData.ftLastAccessTime.dwLowDateTime != 0) { InteropWindows.FileTimeToSystemTime(ref WinFindFileData.ftLastAccessTime, ref accessed); } } else { if (LwFindFileData.time_tLastAccessTime != 0) { InteropLikewise.FileTimeToSystemTime(ref LwFindFileData.time_tLastAccessTime, ref accessed); } } Accessed = new DateTime(accessed.wYear, accessed.wMonth, accessed.wDay, accessed.wHour, accessed.wMinute, accessed.wSecond).ToLocalTime(); file.CreationTime = Created; file.LastWriteTime = Modified; file.LastAccessTime = Accessed; if (size > 0) { file.FileSizeInKB = size; file.FileSmallSizeInBytes = 0; } else { file.FileSizeInKB = 0; file.FileSmallSizeInBytes = extra; } Files.Add(file); if (useWindowsDlls) { success = InteropWindows.FindNextFileW(handle, ref WinFindFileData); } else { success = InteropLikewise.FindNextFile(handle, ref LwFindFileData); } } } catch (Exception ex) { Console.WriteLine("Exception in Likewise.LMC.FileClient.FileClient.EnumFiles " + ex.StackTrace); } return(Files); }
/* * 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; }