Exemplo n.º 1
0
        /*
         * 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;
        }
Exemplo n.º 2
0
        /*
         * 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);
        }
Exemplo n.º 3
0
        /*
         * 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;
        }