Пример #1
0
 /// <summary>
 /// Deletes the specified directory and, if indicated, 
 /// any subdirectories and files in the directory.
 /// </summary>
 /// <param name="path">The path.</param>
 /// <param name="user">The user.</param>
 /// <param name="recursive">if set to <c>true</c> [recursive].</param>
 /// <param name="debug">if set to <c>true</c> [debug].</param>
 /// <returns></returns>
 public static FileOperationResult DeleteDirectory(string path, 
     DomainUser user, bool recursive = false, bool debug = false)
 {
     FileOperationResult result;
     // ReSharper disable once UnusedVariable
     using (
         var impersonation = new Impersonation(user.Domain, user.Name,
             user.Pwd, ImpersonationLevel.Delegation))
     {
         if (Directory.Exists(path))
         {
             try
             {
                 Directory.Delete(path, recursive);
                 result = new FileOperationResult(path,
                     FileOperation.Delete, true);
             }
             catch (Exception ex)
             {
                 Log.Error(ex);
                 result = new FileOperationResult(path,
                     FileOperation.Delete, ex);
             }
         }
         else
         {
             result = new FileOperationResult(path,
                     FileOperation.Delete, false, "Directory not Found");
         }
     }
     return result;
 }
Пример #2
0
 /// <summary>
 /// Deletes the file.
 /// </summary>
 /// <param name="file">The file.</param>
 /// <param name="user">The user.</param>
 /// <param name="debug">if set to <c>true</c> [debug].</param>
 /// <returns></returns>
 public static FileOperationResult DeleteFile(FileInfo file, 
     DomainUser user, bool debug = false)
 {
     FileOperationResult result;
     // ReSharper disable once UnusedVariable
     using (
         var impersonation = new Impersonation(user.Domain, user.Name,
             user.Pwd, ImpersonationLevel.Delegation))
     {
         try
         {
             File.Delete(file.FullName);
             result = new FileOperationResult(file.FullName,
                     FileOperation.Delete, true);
         }
         catch (Exception ex)
         {
             Log.Error(ex);
             result = new FileOperationResult(file.FullName,
                 FileOperation.Delete, ex);
         }
     }
     return result;
 }
Пример #3
0
        private static FileOperationResult MoveFile(FileInfo sourceFile, string destination, bool overwrite,
            bool debug = false)
        {
            FileOperationResult result;
            try
            {
                if (!Directory.Exists(destination))
                {
                    Directory.CreateDirectory(destination);
                    if (debug)
                    {
                        Log.Info($"cdir {destination}");
                    }
                }

                var dst = Path.Combine(destination, sourceFile.Name);

                if (File.Exists(dst) && overwrite)
                {
                    File.Delete(dst);
                }

                if (!File.Exists(dst))
                {
                    File.Move(sourceFile.FullName, dst);

                    result = new FileOperationResult(dst, FileOperation.Move, true);
                }
                else
                {
                    if (debug)
                    {
                        Log.Warning($"file already exists {dst}");
                    }
                    result = new FileOperationResult(dst, FileOperation.Move, false, "file already exists");
                }
            }
            catch (Exception ex)
            {
                Log.Error(ex);
                result = new FileOperationResult(sourceFile.FullName, FileOperation.Move, ex);
            }

            return result;
        }