Beispiel #1
0
        /*
         * 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);
        }
Beispiel #2
0
        /*
         * 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);
        }
Beispiel #3
0
        /*
         * 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);
        }
Beispiel #4
0
        /*
         * 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);
        }
Beispiel #5
0
        /*
         * 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);
        }
Beispiel #6
0
        /*
         * 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);
        }
Beispiel #7
0
        /*
         * 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);
        }
Beispiel #8
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);
        }
Beispiel #9
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;
        }
Beispiel #10
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;
        }