Ejemplo n.º 1
0
        public static void ShellDelete(string FullPath, RecycleOption recycle, UICancelOption OnUserCancel, FileOrDirectory FileOrDirectory)
        {
            ShFileOperationFlags operationFlags = ShFileOperationFlags.FOF_SILENT | ShFileOperationFlags.FOF_NOCONFIRMATION;

            if (recycle == RecycleOption.SendToRecycleBin)
            {
                operationFlags |= ShFileOperationFlags.FOF_ALLOWUNDO;
            }
            ShellFileOperation(SHFileOperationType.FO_DELETE, operationFlags, FullPath, null, OnUserCancel, FileOrDirectory);
        }
Ejemplo n.º 2
0
        private void OnedriveButton_Click(object sender, EventArgs e)
        {
            ModifyRegistry.ModifyRegistry userShellFolders = new ModifyRegistry.ModifyRegistry(Registry.CurrentUser, @"Microsoft", @"Windows\CurrentVersion\Explorer\User Shell Folders");
            ModifyRegistry.ModifyRegistry onedriveFolder   = new ModifyRegistry.ModifyRegistry(Registry.CurrentUser, @"Microsoft", @"Onedrive\Accounts\Business1");

            string oneDriveDesktopPath   = onedriveFolder.ReadString("UserFolder") + @"\Desktop\";
            string oneDriveDocumentsPath = onedriveFolder.ReadString(@"UserFolder") + @"\Documents\";
            string desktopShellPath      = userShellFolders.ReadString(@"{754AC886-DF64-4CBA-86B5-F7FBF4FBCEF5}") + @"\";
            string documentsShellPath    = userShellFolders.ReadString(@"Personal") + @"\";

            try
            {
                if (@desktopShellPath != @oneDriveDesktopPath)
                {
                    UIOption       showUI       = UIOption.AllDialogs;
                    UICancelOption onUserCancel = UICancelOption.ThrowException;
                    FileSystem.CopyDirectory(@desktopShellPath, @oneDriveDesktopPath, showUI, onUserCancel);
                }
            }
            catch (Exception dMoveEx)
            {
                MessageBox.Show(dMoveEx.Message + dMoveEx.TargetSite);
            }

            userShellFolders.WriteString(@"{754AC886-DF64-4CBA-86B5-F7FBF4FBCEF5}", @oneDriveDesktopPath.TrimEnd(char.Parse(@"\")));
            userShellFolders.WriteString(@"Desktop", @oneDriveDesktopPath.TrimEnd(char.Parse(@"\")));

            try
            {
                if (documentsShellPath != oneDriveDocumentsPath)
                {
                    UIOption         showUI       = UIOption.AllDialogs;
                    UICancelOption   onUserCancel = UICancelOption.ThrowException;
                    FileIOPermission f            = new FileIOPermission(PermissionState.Unrestricted);
                    f.AllFiles = FileIOPermissionAccess.AllAccess;
                    FileSystem.CopyDirectory(documentsShellPath, oneDriveDocumentsPath, showUI, onUserCancel);
                }
            }
            catch (Exception dMoveEx)
            {
                MessageBox.Show(dMoveEx.Message);
            }

            userShellFolders.WriteString(@"Personal", oneDriveDocumentsPath.TrimEnd(char.Parse(@"\")));

            RestartProcess.RestartProcessByFileName("explorer");

            UpdateOnedriveInfo();
        }
Ejemplo n.º 3
0
        private void companyLinks_Click(object sender, EventArgs e)
        {
            UIOption       showUI       = UIOption.AllDialogs;
            UICancelOption onUserCancel = UICancelOption.ThrowException;

            try
            {
                FileSystem.CopyDirectory(@"Company Links", @"C:\Users\Default\Desktop\Company Links", showUI, onUserCancel);
                FileSystem.CopyDirectory(@"Company Links", @"C:\Users\" + Environment.UserName + @"\Desktop\Company Links", showUI, onUserCancel);
            }
            catch (Exception ee)
            {
                MessageBox.Show(ee.Message);
            }
        }
 public void DeleteFile(string file, UIOption showUI, RecycleOption recycle, UICancelOption onUserCancel)
 {
     FileSystem.DeleteFile(file, showUI, recycle, onUserCancel);
 }
 public void DeleteDirectory(string directory, UIOption showUI, RecycleOption recycle, UICancelOption onUserCancel)
 {
     FileSystem.DeleteDirectory(directory, showUI, recycle, onUserCancel);
 }
 public void CopyFile(string sourceFileName, string destinationFileName, UIOption showUI, UICancelOption onUserCancel)
 {
     FileSystem.CopyFile(sourceFileName, destinationFileName, showUI, onUserCancel);
 }
 private static void VerifyUICancelOption(string argName, UICancelOption argValue)
 {
     if ((argValue != UICancelOption.DoNothing) && (argValue != UICancelOption.ThrowException))
     {
         throw new InvalidEnumArgumentException(argName, (int) argValue, typeof(UICancelOption));
     }
 }
Ejemplo n.º 8
0
 public void DeleteDirectory(string directory, UIOption showUI, RecycleOption recycle, UICancelOption onUserCancel)
 {
     FileSystem.DeleteDirectory(directory, showUI, recycle, onUserCancel);
 }
Ejemplo n.º 9
0
 public void CopyDirectory(string sourceDirectoryName, string destinationDirectoryName, UIOption showUI, UICancelOption onUserCancel)
 {
     FileSystem.CopyDirectory(sourceDirectoryName, destinationDirectoryName, showUI, onUserCancel);
 }
 public static void CopyDirectory(string sourceDirectoryName, string destinationDirectoryName, UIOption showUI, UICancelOption onUserCancel)
 {
     CopyOrMoveDirectory(CopyOrMove.Copy, sourceDirectoryName, destinationDirectoryName, false, ToUIOptionInternal(showUI), onUserCancel);
 }
 public static void MoveFile(string sourceFileName, string destinationFileName, UIOption showUI, UICancelOption onUserCancel)
 {
     CopyOrMoveFile(CopyOrMove.Move, sourceFileName, destinationFileName, false, ToUIOptionInternal(showUI), onUserCancel);
 }
 private static void DeleteFileInternal(string file, UIOptionInternal showUI, RecycleOption recycle, UICancelOption onUserCancel)
 {
     VerifyRecycleOption("recycle", recycle);
     VerifyUICancelOption("onUserCancel", onUserCancel);
     string path = NormalizeFilePath(file, "file");
     new FileIOPermission(FileIOPermissionAccess.Write, path).Demand();
     ThrowIfDevicePath(path);
     if (!File.Exists(path))
     {
         throw ExceptionUtils.GetFileNotFoundException(file, "IO_FileNotFound_Path", new string[] { file });
     }
     if ((showUI != UIOptionInternal.NoUI) && Environment.UserInteractive)
     {
         ShellDelete(path, showUI, recycle, onUserCancel, FileOrDirectory.File);
     }
     else
     {
         File.Delete(path);
     }
 }
 public static void DeleteFile(string file, UIOption showUI, RecycleOption recycle, UICancelOption onUserCancel)
 {
     DeleteFileInternal(file, ToUIOptionInternal(showUI), recycle, onUserCancel);
 }
 private static void DeleteDirectoryInternal(string directory, DeleteDirectoryOption onDirectoryNotEmpty, UIOptionInternal showUI, RecycleOption recycle, UICancelOption onUserCancel)
 {
     VerifyDeleteDirectoryOption("onDirectoryNotEmpty", onDirectoryNotEmpty);
     VerifyRecycleOption("recycle", recycle);
     VerifyUICancelOption("onUserCancel", onUserCancel);
     string fullPath = Path.GetFullPath(directory);
     DemandDirectoryPermission(fullPath, FileIOPermissionAccess.Write);
     ThrowIfDevicePath(fullPath);
     if (!Directory.Exists(fullPath))
     {
         throw ExceptionUtils.GetDirectoryNotFoundException("IO_DirectoryNotFound_Path", new string[] { directory });
     }
     if (IsRoot(fullPath))
     {
         throw ExceptionUtils.GetIOException("IO_DirectoryIsRoot_Path", new string[] { directory });
     }
     if ((showUI != UIOptionInternal.NoUI) && Environment.UserInteractive)
     {
         ShellDelete(fullPath, showUI, recycle, onUserCancel, FileOrDirectory.Directory);
     }
     else
     {
         Directory.Delete(fullPath, onDirectoryNotEmpty == DeleteDirectoryOption.DeleteAllContents);
     }
 }
 public static void DeleteDirectory(string directory, UIOption showUI, RecycleOption recycle, UICancelOption onUserCancel)
 {
     DeleteDirectoryInternal(directory, DeleteDirectoryOption.DeleteAllContents, ToUIOptionInternal(showUI), recycle, onUserCancel);
 }
 private static void CopyOrMoveFile(CopyOrMove operation, string sourceFileName, string destinationFileName, bool overwrite, UIOptionInternal showUI, UICancelOption onUserCancel)
 {
     VerifyUICancelOption("onUserCancel", onUserCancel);
     string path = NormalizeFilePath(sourceFileName, "sourceFileName");
     string str = NormalizeFilePath(destinationFileName, "destinationFileName");
     FileIOPermissionAccess read = FileIOPermissionAccess.Read;
     if (operation == CopyOrMove.Move)
     {
         read |= FileIOPermissionAccess.Write;
     }
     new FileIOPermission(read, path).Demand();
     new FileIOPermission(FileIOPermissionAccess.Write, str).Demand();
     ThrowIfDevicePath(path);
     ThrowIfDevicePath(str);
     if (!File.Exists(path))
     {
         throw ExceptionUtils.GetFileNotFoundException(sourceFileName, "IO_FileNotFound_Path", new string[] { sourceFileName });
     }
     if (Directory.Exists(str))
     {
         throw ExceptionUtils.GetIOException("IO_DirectoryExists_Path", new string[] { destinationFileName });
     }
     Directory.CreateDirectory(GetParentPath(str));
     if ((showUI != UIOptionInternal.NoUI) && Environment.UserInteractive)
     {
         ShellCopyOrMove(operation, FileOrDirectory.File, path, str, showUI, onUserCancel);
     }
     else if ((operation == CopyOrMove.Copy) || path.Equals(str, StringComparison.OrdinalIgnoreCase))
     {
         File.Copy(path, str, overwrite);
     }
     else if (overwrite)
     {
         if (Environment.OSVersion.Platform == PlatformID.Win32NT)
         {
             new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
             try
             {
                 if (!Microsoft.VisualBasic.CompilerServices.NativeMethods.MoveFileEx(path, str, 11))
                 {
                     ThrowWinIOError(Marshal.GetLastWin32Error());
                 }
             }
             catch (Exception)
             {
                 throw;
             }
             finally
             {
                 CodeAccessPermission.RevertAssert();
             }
         }
         else
         {
             File.Delete(str);
             File.Move(path, str);
         }
     }
     else
     {
         File.Move(path, str);
     }
 }
Ejemplo n.º 17
0
        public static void ShellFileOperation(SHFileOperationType OperationType, ShFileOperationFlags OperationFlags, string FullSource, string FullTarget, UICancelOption OnUserCancel, FileOrDirectory FileOrDirectory)
        {
            int num;

            new UIPermission(UIPermissionWindow.SafeSubWindows).Demand();
            FileIOPermissionAccess noAccess = FileIOPermissionAccess.NoAccess;

            if (OperationType == SHFileOperationType.FO_COPY)
            {
                noAccess = FileIOPermissionAccess.Read;
            }
            else if (OperationType == SHFileOperationType.FO_MOVE)
            {
                noAccess = FileIOPermissionAccess.Write | FileIOPermissionAccess.Read;
            }
            else if (OperationType == SHFileOperationType.FO_DELETE)
            {
                noAccess = FileIOPermissionAccess.Write;
            }
            string fullDirectoryPath = FullSource;

            if (((OperationType == SHFileOperationType.FO_COPY) || (OperationType == SHFileOperationType.FO_MOVE)) && fullDirectoryPath.EndsWith("*", StringComparison.Ordinal))
            {
                fullDirectoryPath = RemoveEndingSeparator(FullSource.TrimEnd(new char[] { '*' }));
            }
            if (FileOrDirectory == FileOrDirectory.Directory)
            {
                DemandDirectoryPermission(fullDirectoryPath, noAccess);
            }
            else
            {
                new FileIOPermission(noAccess, fullDirectoryPath).Demand();
            }
            if (OperationType != SHFileOperationType.FO_DELETE)
            {
                if (FileOrDirectory == FileOrDirectory.Directory)
                {
                    DemandDirectoryPermission(FullTarget, FileIOPermissionAccess.Write);
                }
                else
                {
                    new FileIOPermission(FileIOPermissionAccess.Write, FullTarget).Demand();
                }
            }
            SHFILEOPSTRUCT lpFileOp = GetShellOperationInfo(OperationType, OperationFlags, FullSource, FullTarget);

            new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
            try
            {
                num = SHFileOperation(ref lpFileOp);
                SHChangeNotify(145439, 3, IntPtr.Zero, IntPtr.Zero);
            }
            catch (Exception)
            {
                throw;
            }
            finally
            {
                CodeAccessPermission.RevertAssert();
            }
            if (lpFileOp.fAnyOperationsAborted)
            {
                if (OnUserCancel == UICancelOption.ThrowException)
                {
                    throw new OperationCanceledException();
                }
            }
            else if (num != 0)
            {
                ThrowWinIOError(num);
            }
        }
 private static void ShellCopyOrMove(CopyOrMove Operation, FileOrDirectory TargetType, string FullSourcePath, string FullTargetPath, UIOptionInternal ShowUI, UICancelOption OnUserCancel)
 {
     Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType type;
     if (Operation == CopyOrMove.Copy)
     {
         type = Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_COPY;
     }
     else
     {
         type = Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_MOVE;
     }
     Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags operationFlags = GetOperationFlags(ShowUI);
     string fullSource = FullSourcePath;
     if (TargetType == FileOrDirectory.Directory)
     {
         if (Directory.Exists(FullTargetPath))
         {
             fullSource = Path.Combine(FullSourcePath, "*");
         }
         else
         {
             Directory.CreateDirectory(GetParentPath(FullTargetPath));
         }
     }
     ShellFileOperation(type, operationFlags, fullSource, FullTargetPath, OnUserCancel, TargetType);
     if ((((Operation == CopyOrMove.Move) & (TargetType == FileOrDirectory.Directory)) && Directory.Exists(FullSourcePath)) && ((Directory.GetDirectories(FullSourcePath).Length == 0) && (Directory.GetFiles(FullSourcePath).Length == 0)))
     {
         Directory.Delete(FullSourcePath, false);
     }
 }
Ejemplo n.º 19
0
 public void CopyFile(string sourceFileName, string destinationFileName, UIOption showUI, UICancelOption onUserCancel)
 {
     FileSystem.CopyFile(sourceFileName, destinationFileName, showUI, onUserCancel);
 }
 private static void ShellDelete(string FullPath, UIOptionInternal ShowUI, RecycleOption recycle, UICancelOption OnUserCancel, FileOrDirectory FileOrDirectory)
 {
     Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags operationFlags = GetOperationFlags(ShowUI);
     if (recycle == RecycleOption.SendToRecycleBin)
     {
         operationFlags |= Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags.FOF_ALLOWUNDO;
     }
     ShellFileOperation(Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_DELETE, operationFlags, FullPath, null, OnUserCancel, FileOrDirectory);
 }
Ejemplo n.º 21
0
 public void DeleteFile(string file, UIOption showUI, RecycleOption recycle, UICancelOption onUserCancel)
 {
     FileSystem.DeleteFile(file, showUI, recycle, onUserCancel);
 }
 private static void ShellFileOperation(Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType OperationType, Microsoft.VisualBasic.CompilerServices.NativeMethods.ShFileOperationFlags OperationFlags, string FullSource, string FullTarget, UICancelOption OnUserCancel, FileOrDirectory FileOrDirectory)
 {
     int num;
     new UIPermission(UIPermissionWindow.SafeSubWindows).Demand();
     FileIOPermissionAccess noAccess = FileIOPermissionAccess.NoAccess;
     if (OperationType == Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_COPY)
     {
         noAccess = FileIOPermissionAccess.Read;
     }
     else if (OperationType == Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_MOVE)
     {
         noAccess = FileIOPermissionAccess.Write | FileIOPermissionAccess.Read;
     }
     else if (OperationType == Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_DELETE)
     {
         noAccess = FileIOPermissionAccess.Write;
     }
     string fullDirectoryPath = FullSource;
     if (((OperationType == Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_COPY) || (OperationType == Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_MOVE)) && fullDirectoryPath.EndsWith("*", StringComparison.Ordinal))
     {
         fullDirectoryPath = RemoveEndingSeparator(FullSource.TrimEnd(new char[] { '*' }));
     }
     if (FileOrDirectory == FileSystem.FileOrDirectory.Directory)
     {
         DemandDirectoryPermission(fullDirectoryPath, noAccess);
     }
     else
     {
         new FileIOPermission(noAccess, fullDirectoryPath).Demand();
     }
     if (OperationType != Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperationType.FO_DELETE)
     {
         if (FileOrDirectory == FileSystem.FileOrDirectory.Directory)
         {
             DemandDirectoryPermission(FullTarget, FileIOPermissionAccess.Write);
         }
         else
         {
             new FileIOPermission(FileIOPermissionAccess.Write, FullTarget).Demand();
         }
     }
     Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFILEOPSTRUCT lpFileOp = GetShellOperationInfo(OperationType, OperationFlags, FullSource, FullTarget);
     new SecurityPermission(SecurityPermissionFlag.UnmanagedCode).Assert();
     try
     {
         num = Microsoft.VisualBasic.CompilerServices.NativeMethods.SHFileOperation(ref lpFileOp);
         Microsoft.VisualBasic.CompilerServices.NativeMethods.SHChangeNotify(0x2381f, 3, IntPtr.Zero, IntPtr.Zero);
     }
     catch (Exception)
     {
         throw;
     }
     finally
     {
         CodeAccessPermission.RevertAssert();
     }
     if (lpFileOp.fAnyOperationsAborted)
     {
         if (OnUserCancel == UICancelOption.ThrowException)
         {
             throw new OperationCanceledException();
         }
     }
     else if (num != 0)
     {
         ThrowWinIOError(num);
     }
 }
 public void CopyDirectory(string sourceDirectoryName, string destinationDirectoryName, UIOption showUI, UICancelOption onUserCancel)
 {
     FileSystem.CopyDirectory(sourceDirectoryName, destinationDirectoryName, showUI, onUserCancel);
 }
 private static void CopyOrMoveDirectory(CopyOrMove operation, string sourceDirectoryName, string destinationDirectoryName, bool overwrite, UIOptionInternal showUI, UICancelOption onUserCancel)
 {
     VerifyUICancelOption("onUserCancel", onUserCancel);
     string fullDirectoryPath = NormalizePath(sourceDirectoryName);
     string str2 = NormalizePath(destinationDirectoryName);
     FileIOPermissionAccess read = FileIOPermissionAccess.Read;
     if (operation == CopyOrMove.Move)
     {
         read |= FileIOPermissionAccess.Write;
     }
     DemandDirectoryPermission(fullDirectoryPath, read);
     DemandDirectoryPermission(str2, FileIOPermissionAccess.Write | FileIOPermissionAccess.Read);
     ThrowIfDevicePath(fullDirectoryPath);
     ThrowIfDevicePath(str2);
     if (!Directory.Exists(fullDirectoryPath))
     {
         throw ExceptionUtils.GetDirectoryNotFoundException("IO_DirectoryNotFound_Path", new string[] { sourceDirectoryName });
     }
     if (IsRoot(fullDirectoryPath))
     {
         throw ExceptionUtils.GetIOException("IO_DirectoryIsRoot_Path", new string[] { sourceDirectoryName });
     }
     if (File.Exists(str2))
     {
         throw ExceptionUtils.GetIOException("IO_FileExists_Path", new string[] { destinationDirectoryName });
     }
     if (str2.Equals(fullDirectoryPath, StringComparison.OrdinalIgnoreCase))
     {
         throw ExceptionUtils.GetIOException("IO_SourceEqualsTargetDirectory", new string[0]);
     }
     if (((str2.Length > fullDirectoryPath.Length) && str2.Substring(0, fullDirectoryPath.Length).Equals(fullDirectoryPath, StringComparison.OrdinalIgnoreCase)) && (str2[fullDirectoryPath.Length] == Path.DirectorySeparatorChar))
     {
         throw ExceptionUtils.GetInvalidOperationException("IO_CyclicOperation", new string[0]);
     }
     if ((showUI != UIOptionInternal.NoUI) && Environment.UserInteractive)
     {
         ShellCopyOrMove(operation, FileOrDirectory.Directory, fullDirectoryPath, str2, showUI, onUserCancel);
     }
     else
     {
         FxCopyOrMoveDirectory(operation, fullDirectoryPath, str2, overwrite);
     }
 }