Пример #1
0
        public void TestGetAccessControlSections()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                var security = Directory.GetAccessControl(tempLongPathFilename, AccessControlSections.Access);
                Assert.IsNotNull(security);
                Assert.AreEqual(typeof(FileSystemRights), security.AccessRightType);
                Assert.AreEqual(typeof(FileSystemAccessRule), security.AccessRuleType);
                Assert.AreEqual(typeof(FileSystemAuditRule), security.AuditRuleType);
                Assert.IsTrue(security.AreAccessRulesCanonical);
                Assert.IsTrue(security.AreAuditRulesCanonical);
                Assert.IsFalse(security.AreAccessRulesProtected);
                Assert.IsFalse(security.AreAuditRulesProtected);
                var securityGetAccessRules = security.GetAuditRules(true, true, typeof(System.Security.Principal.NTAccount)).Cast <FileSystemAccessRule>();
                Assert.AreEqual(0, securityGetAccessRules.Count());
                AuthorizationRuleCollection perm = security.GetAccessRules(true, true, typeof(System.Security.Principal.NTAccount));
                var ntAccount             = new System.Security.Principal.NTAccount(System.Security.Principal.WindowsIdentity.GetCurrent().Name);
                FileSystemAccessRule rule = perm.Cast <FileSystemAccessRule>().SingleOrDefault(e => ntAccount == e.IdentityReference);
                Assert.IsNotNull(rule);
                Assert.IsTrue((rule.FileSystemRights & FileSystemRights.FullControl) != 0);
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #2
0
        public void TestInUseMove()
        {
            const bool recursive = true;

#if SHORT_SOURCE
            var tempPathFilename1 = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), System.IO.Path.GetRandomFileName());
            System.IO.Directory.CreateDirectory(tempPathFilename1);
            Assert.IsTrue(System.IO.Directory.Exists(Path.GetFullPath(tempPathFilename1)));
            var tempPathFilename2 = System.IO.Path.Combine(System.IO.Directory.GetCurrentDirectory(), System.IO.Path.GetRandomFileName());
            System.IO.Directory.CreateDirectory(tempPathFilename2);
            Assert.IsTrue(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename2)));
            try
            {
                using (
                    var writer = System.IO.File.CreateText(System.IO.Path.Combine(tempPathFilename2, "TestInUseMove")))
                {
                    string destinationPath =
                        System.IO.Path.GetFullPath(System.IO.Path.Combine(tempPathFilename1, System.IO.Path.GetFileName(tempPathFilename2)));
                    System.IO.Directory.Move(tempPathFilename2, destinationPath);
                    Assert.IsTrue(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename1)));
                    Assert.IsFalse(System.IO.Directory.Exists(System.IO.Path.GetFullPath(tempPathFilename2)));
                    Assert.IsTrue(System.IO.Directory.Exists(destinationPath));
                }
            }
            catch (Exception e)
            {
                throw;
            }
            finally
            {
                Directory.Delete(tempPathFilename1, recursive);
                Directory.Delete(tempPathFilename2, recursive);
            }
#endif
            var tempLongPathFilename1 = Path.Combine(uncDirectory, Path.GetRandomFileName());
            Directory.CreateDirectory(tempLongPathFilename1);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
            var tempLongPathFilename2 = Path.Combine(uncDirectory, Path.GetRandomFileName());
            Directory.CreateDirectory(tempLongPathFilename2);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));
            try
            {
                using (
                    var writer = File.CreateText(Path.Combine(tempLongPathFilename2, "TestInUseMove")))
                {
                    string destinationPath =
                        Path.GetFullPath(Path.Combine(tempLongPathFilename1, Path.GetFileName(tempLongPathFilename2)));
                    Directory.Move(tempLongPathFilename2, destinationPath);
                    Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
                    Assert.IsFalse(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));
                    Assert.IsTrue(Directory.Exists(destinationPath));
                }
            }
            finally
            {
                Directory.Delete(tempLongPathFilename1, recursive);
                Directory.Delete(tempLongPathFilename2, recursive);
            }
        }
Пример #3
0
        public void TestDirectoryCreateNearMaxPathLimit()
        {
            var uncPathNearMaxPathLimit = Path.Combine(uncDirectory, new string('x', Pri.LongPath.NativeMethods.MAX_PATH - uncDirectory.Length - 2));

            Directory.CreateDirectory(uncPathNearMaxPathLimit);
            Assert.That(Directory.Exists(uncPathNearMaxPathLimit));
            Directory.Delete(uncPathNearMaxPathLimit);
        }
Пример #4
0
        public void TestDeleteDirectory()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename)));
            Directory.Delete(tempLongPathFilename);
            Assert.IsFalse(Directory.Exists(Path.GetFullPath(tempLongPathFilename)));
        }
        public static bool CheckAccessToPath(string iPath)
        {
            string UniqueDirName = iPath + "kumo@Path@Check@Dir";

            try {
                Directory.CreateDirectory(UniqueDirName);
                Directory.Delete(UniqueDirName);
                return(true);
            } catch (Exception e) {
                return(false);
            }
        }
Пример #6
0
        /// <summary>
        /// Deletes a junction point at the specified source directory along with the directory itself.
        /// Does nothing if the junction point does not exist.
        /// </summary>
        /// <remarks>
        /// Only works on NTFS.
        /// </remarks>
        /// <param name="junctionPoint">The junction point path</param>
        public static void Delete(string junctionPoint)
        {
            if (!Directory.Exists(junctionPoint))
            {
                if (File.Exists(junctionPoint))
                {
                    throw new IOException("Path is not a junction point.");
                }

                return;
            }

            using (SafeFileHandle handle = OpenReparsePoint(junctionPoint, EFileAccess.GenericWrite))
            {
                var reparseDataBuffer = new REPARSE_DATA_BUFFER
                {
                    ReparseTag        = IO_REPARSE_TAG_MOUNT_POINT,
                    ReparseDataLength = 0,
                    PathBuffer        = new byte[0x3ff0]
                };


                int    inBufferSize = Marshal.SizeOf(reparseDataBuffer);
                IntPtr inBuffer     = Marshal.AllocHGlobal(inBufferSize);
                try
                {
                    Marshal.StructureToPtr(reparseDataBuffer, inBuffer, false);

                    int  bytesReturned;
                    bool result = DeviceIoControl(handle.DangerousGetHandle(), FSCTL_DELETE_REPARSE_POINT,
                                                  inBuffer, 8, IntPtr.Zero, 0, out bytesReturned, IntPtr.Zero);

                    if (!result)
                    {
                        ThrowLastWin32Error("Unable to delete junction point.");
                    }
                }
                finally
                {
                    Marshal.FreeHGlobal(inBuffer);
                }

                try
                {
                    Directory.Delete(junctionPoint);
                }
                catch (IOException ex)
                {
                    throw new IOException("Unable to delete junction point.", ex);
                }
            }
        }
Пример #7
0
        public IResult <Result.InternalTypes.Void> DeleteDirectory(string path, bool recursive)
        {
            try
            {
                Directory.Delete(path, recursive);

                return(new SuccessResult());
            }
            catch (Exception e)
            {
                return(new FailureResult(e));
            }
        }
Пример #8
0
 static void CleanupTempFolders()
 {
     if (Directory.Exists(Paths.TransferPath))
     {
         MarkFolderWritable(Paths.TransferPath);
         Directory.Delete(Paths.TransferPath, true);
     }
     if (Directory.Exists(Paths.RamDriveTransferPath))
     {
         MarkFolderWritable(Paths.RamDriveTransferPath);
         Directory.Delete(Paths.RamDriveTransferPath, true);
     }
 }
Пример #9
0
        public void TestCreateWithFileSecurity()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            try
            {
                Directory.CreateDirectory(tempLongPathFilename, new DirectorySecurity());
                Assert.IsTrue(Directory.Exists(tempLongPathFilename));
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #10
0
        public void TestReplaceIgnoreMergeWithReadonlyBackupPath()
        {
            var tempLongPathFilename = new StringBuilder(longPathDirectory).Append(@"\").Append("filename.ext").ToString();
            var tempBackupPathName   = new StringBuilder(longPathDirectory).Append(@"\readonly").ToString();
            var di = new DirectoryInfo(tempBackupPathName);

            di.Create();

            var attr = di.Attributes;

            di.Attributes = attr | FileAttributes.ReadOnly;
            var tempBackupLongPathFilename = new StringBuilder(tempBackupPathName).Append(@"\").Append("backup").ToString();

            using (var fileStream = File.Create(tempLongPathFilename))
            {
                fileStream.WriteByte(42);
            }
            var tempLongPathFilename2 = new StringBuilder(longPathDirectory).Append(@"\").Append("filename2.ext").ToString();

            using (var fileStream = File.Create(tempLongPathFilename2))
            {
                fileStream.WriteByte(52);
            }
            try
            {
                const bool ignoreMetadataErrors = true;
                File.Replace(tempLongPathFilename, tempLongPathFilename2, tempBackupLongPathFilename, ignoreMetadataErrors);
                using (var fileStream = File.OpenRead(tempLongPathFilename2))
                {
                    Assert.AreEqual(42, fileStream.ReadByte());
                }
                Assert.IsFalse(File.Exists(tempLongPathFilename));
                Assert.IsTrue(File.Exists(tempBackupLongPathFilename));
            }
            finally
            {
                di.Attributes = attr;
                if (File.Exists(tempLongPathFilename))
                {
                    File.Delete(tempLongPathFilename);
                }
                File.Delete(tempLongPathFilename2);
                File.Delete(tempBackupLongPathFilename);
                if (Directory.Exists(tempBackupPathName))
                {
                    Directory.Delete(tempBackupPathName);
                }
            }
        }
Пример #11
0
        public void TestCreateDirectoryThatEndsWithSlash()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName() + @"\");
            var di = Directory.CreateDirectory(tempLongPathFilename);

            try
            {
                Assert.IsNotNull(di);
                Assert.IsTrue(Directory.Exists(tempLongPathFilename));
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #12
0
        public void TestGetCreationTime()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                var dateTime = Directory.GetCreationTime(tempLongPathFilename);
                var fi       = new DirectoryInfo(tempLongPathFilename);
                Assert.AreEqual(fi.CreationTime, dateTime);
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #13
0
        public void TestEnumerateDirectoriesWithSearch()
        {
            var randomFileName       = Path.GetRandomFileName();
            var tempLongPathFilename = Path.Combine(uncDirectory, randomFileName);

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                var dirs = Directory.EnumerateDirectories(uncDirectory, "*").ToArray();
                Assert.AreEqual(1, dirs.Length);
                Assert.IsTrue(dirs.Contains(tempLongPathFilename));
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #14
0
        public void TestSetLastAccessTime()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                DateTime dateTime = DateTime.Now.AddDays(1);
                Directory.SetLastAccessTime(tempLongPathFilename, dateTime);
                var fi = new DirectoryInfo(tempLongPathFilename);
                Assert.AreEqual(fi.LastAccessTime, dateTime);
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #15
0
        public void TestSetCreationTimeUtc()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                DateTime dateTime = DateTime.UtcNow.AddDays(1);
                Directory.SetCreationTimeUtc(tempLongPathFilename, dateTime);
                var di = new DirectoryInfo(tempLongPathFilename);
                Assert.AreEqual(di.CreationTimeUtc, dateTime);
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #16
0
        public void TestRecursiveEnumerateDirectoriesWithSingleSubsetSearch()
        {
            var randomFileName       = "TestRecursiveEnumerateDirectoriesWithSubsetSearch";
            var tempLongPathFilename = Path.Combine(uncDirectory, randomFileName);

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                var dirs = Directory.EnumerateDirectories(uncDirectory, "T*", SearchOption.AllDirectories).ToArray();
                Assert.AreEqual(1, dirs.Length);
                Assert.IsTrue(dirs.Contains(tempLongPathFilename));
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #17
0
        public void TestGetDirectoriesWithSearchWithNoResults()
        {
            var randomFileName       = Path.GetRandomFileName();
            var tempLongPathFilename = Path.Combine(uncDirectory, randomFileName);

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                var dirs = Directory.GetDirectories(uncDirectory, "gibberish").ToArray();
                Assert.AreEqual(0, dirs.Length);
                Assert.IsFalse(dirs.Contains(tempLongPathFilename));
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #18
0
        public void TestGetRecursiveDirectoriesWithSubsetSearch()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, "TestGetRecursiveDirectoriesWithSubsetSearch");

            Directory.CreateDirectory(tempLongPathFilename);
            var tempLongPathFilename2 = Path.Combine(tempLongPathFilename, "ATestGetRecursiveDirectoriesWithSubsetSearch");

            Directory.CreateDirectory(tempLongPathFilename2);
            try
            {
                Assert.AreEqual(1, Directory.GetDirectories(uncDirectory, "A*", System.IO.SearchOption.AllDirectories).Count());
            }
            finally
            {
                Directory.Delete(tempLongPathFilename2);
                Directory.Delete(tempLongPathFilename);
            }
        }
Пример #19
0
        public void TestGetDirectoriesWithAnySearch()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, "TestGetDirectoriesWithAnySearch");

            Directory.CreateDirectory(tempLongPathFilename);
            var tempLongPathFilename2 = Path.Combine(uncDirectory, "ATestGetDirectoriesWithAnySearch");

            Directory.CreateDirectory(tempLongPathFilename2);
            try
            {
                Assert.AreEqual(2, Directory.GetDirectories(uncDirectory, "*").Count());
            }
            finally
            {
                Directory.Delete(tempLongPathFilename);
                Directory.Delete(tempLongPathFilename2);
            }
        }
        // clean all files and directories in iPath, then delete iPath dir...
        public static void DeleteDirectory(string iPath, string[] ignoreFiles = null)
        {
            if (iPath == null)
            {
                return;
            }
            if (Application.platform == RuntimePlatform.WindowsEditor)
            {
                iPath = iPath.Replace('/', '\\');
            }

            if (!Directory.Exists(iPath))
            {
                return;
            }

            // delete subDir
            string[] dirPaths = Directory.GetDirectories(iPath);
            for (int i = 0; i < dirPaths.Length; i++)
            {
                DeleteDirectory(dirPaths[i], ignoreFiles);
            }

            // delete files
            string[] filePaths = Directory.GetFiles(iPath);
            for (int i = 0; i < filePaths.Length; ++i)
            {
                string extension = Path.GetExtension(filePaths[i]);
                if (ignoreFiles != null && Array.IndexOf(ignoreFiles, extension) >= 0)
                {
                    continue;
                }
                if (filePaths[i] != null)
                {
                    File.Delete(filePaths[i]);
                }
            }

            // delete iPath dir...
            if (Directory.GetDirectories(iPath).Length == 0 && Directory.GetFiles(iPath).Length == 0)
            {
                Directory.Delete(iPath);
            }
        }
Пример #21
0
        public void TestDirectoryEnumerateDirectoriesNearMaxPathLimit()
        {
            var uncPathNearMaxPathLimit = Path.Combine(uncDirectory, new string('x', Pri.LongPath.NativeMethods.MAX_PATH - uncDirectory.Length - 2));

            Directory.CreateDirectory(uncPathNearMaxPathLimit.Replace(uncDirectory, directory));

            var uncPathAboveMaxPathLimit = Path.Combine(uncPathNearMaxPathLimit, "wibble");

            Directory.CreateDirectory(uncPathAboveMaxPathLimit);

            Assert.That(Directory.Exists(uncPathNearMaxPathLimit));
            Assert.That(Directory.Exists(uncPathAboveMaxPathLimit));

            // there should be one subdirectory inside almostLongPath
            var subDirs = Directory.EnumerateDirectories(uncPathNearMaxPathLimit).ToArray();

            Directory.Delete(uncPathAboveMaxPathLimit);
            Directory.Delete(uncPathNearMaxPathLimit);

            Assert.That(subDirs.Length, Is.EqualTo(1));
        }
Пример #22
0
        public void TestGetFileSystemEntriesRecursiveWithSearchWithNoResults()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                Assert.IsTrue(Directory.Exists(tempLongPathFilename));
                var randomFileName = Util.CreateNewEmptyFile(tempLongPathFilename);

                var files = Directory.GetFileSystemEntries(uncDirectory, "gibberish", SearchOption.AllDirectories).ToArray();
                Assert.AreEqual(0, files.Length);
                Assert.IsFalse(files.Contains(uncFilePath));
                Assert.IsFalse(files.Contains(randomFileName));
            }
            finally
            {
                const bool recursive = true;
                Directory.Delete(tempLongPathFilename, recursive);
            }
        }
Пример #23
0
        public void TestEnumerateRecursiveFileSystemEntriesWithSearch()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                Assert.IsTrue(Directory.Exists(tempLongPathFilename));
                var randomFileName = Util.CreateNewEmptyFile(tempLongPathFilename);

                var entries = Directory.EnumerateFileSystemEntries(uncDirectory, "*", SearchOption.AllDirectories).ToArray();
                Assert.AreEqual(3, entries.Length);
                Assert.IsTrue(entries.Contains(uncFilePath));
                Assert.IsTrue(entries.Contains(randomFileName));
            }
            finally
            {
                const bool recursive = true;
                Directory.Delete(tempLongPathFilename, recursive);
            }
        }
Пример #24
0
 public void TearDown()
 {
     try
     {
         if (File.Exists(filePath))
         {
             File.Delete(filePath);
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine("Exception {0} deleting \"filePath\"", e.ToString());
         throw;
     }
     finally
     {
         if (Directory.Exists(directory))
         {
             Directory.Delete(directory, true);
         }
     }
 }
Пример #25
0
 public void TearDown()
 {
     try
     {
         if (File.Exists(longPathFilename))
         {
             File.Delete(longPathFilename);
         }
     }
     catch (Exception e)
     {
         Trace.WriteLine("Exception {0} deleting \"longPathFilename\"", e.ToString());
         throw;
     }
     finally
     {
         if (Directory.Exists(longPathRoot))
         {
             Directory.Delete(longPathRoot, true);
         }
     }
 }
Пример #26
0
        public void TestMove()
        {
            var tempLongPathFilename1 = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename1);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
            var tempLongPathFilename2 = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename2);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));

            string destinationPath = Path.GetFullPath(Path.Combine(tempLongPathFilename1, Path.GetFileName(tempLongPathFilename2)));

            Directory.Move(tempLongPathFilename2, destinationPath);
            Assert.IsTrue(Directory.Exists(Path.GetFullPath(tempLongPathFilename1)));
            Assert.IsFalse(Directory.Exists(Path.GetFullPath(tempLongPathFilename2)));
            Assert.IsTrue(Directory.Exists(destinationPath));

            const bool recursive = true;

            Directory.Delete(tempLongPathFilename1, recursive);
            Directory.Delete(tempLongPathFilename2, recursive);
        }
Пример #27
0
        public void TestGetRecursiveFilesWithSubsetSearch()
        {
            var tempLongPathFilename = Path.Combine(uncDirectory, Path.GetRandomFileName());

            Directory.CreateDirectory(tempLongPathFilename);
            try
            {
                Assert.IsTrue(Directory.Exists(tempLongPathFilename));
                var randomFileName = Util.CreateNewEmptyFile(tempLongPathFilename);

                var searchPattern = Path.GetFileName(randomFileName).Substring(0, 3) + "*" + Path.GetExtension(randomFileName);

                var files = Directory.GetFiles(uncDirectory, searchPattern, SearchOption.AllDirectories).ToArray();
                Assert.AreEqual(1, files.Length);
                Assert.IsFalse(files.Contains(uncFilePath));
                Assert.IsTrue(files.Contains(randomFileName));
            }
            finally
            {
                const bool recursive = true;
                Directory.Delete(tempLongPathFilename, recursive);
            }
        }
Пример #28
0
        static void ProcessArchive(string rootPath, string folder, string file, IEnumerable <string> files, string destFolder)
        {
            if (Regex.IsMatch(file, @"(?i)\.(rar|r00|zip)$"))
            {
                var zipSize         = GetAssociatedArchives(file, files).Sum(archiveFile => new FileInfo(archiveFile).Length);
                var tempPath        = GetTempPath(zipSize * 2);
                var zipContentsPath = tempPath + folder.Substring(rootPath.Length) + Path.GetFileName(file);
                Console.WriteLine("Extracting {0} => {1}", Path.GetFileName(file), zipContentsPath);
                if (StartProcess(@"C:\Program Files\WinRAR\WinRar.exe", $"x -o+ -ibck -inul -y \"{file}\" \"{zipContentsPath}\"\\") < 2 && Directory.Exists(zipContentsPath))
                {
                    MarkFolderWritable(zipContentsPath);
                    ProcessFolder(tempPath, zipContentsPath, destFolder, true, false);
                }
                else
                {
                    Console.WriteLine("Error while extracting {0}", file);
                    throw new UnzipError();
                }
                Directory.Delete(zipContentsPath, true);
            }

            /*
             * else if (Path.GetExtension(file) == ".iso")
             * {
             *      Console.WriteLine("Mounting {0} => G:", file);
             *      StartProcess(@"C:\Program Files\PowerISO\piso.exe", string.Format("mount \"{0}\" G:", file));
             *      var isoSize = new FileInfo(file).Length;
             *      var tempPath = GetTempPath(isoSize);
             *      var isoContentsPath = tempPath + folder.Substring(rootPath.Length);
             *      CopyFiles("G:", isoContentsPath, false);
             *      MarkFolderWritable(isoContentsPath);
             *      ProcessFolder(tempPath, isoContentsPath, destFolder, true, true, string.Format(@"\{0}", Path.GetFileName(file)));
             *      Unmount();
             *      Directory.Delete(isoContentsPath, true);
             * }
             * */
        }
Пример #29
0
 public void Delete(string path, bool recursive)
 {
     Directory.Delete(path, recursive);
 }
        public void DecryptCourse(List <ListViewItem> list)
        {
            if (string.IsNullOrWhiteSpace(txtCoursePath.Text))
            {
                MessageBox.Show("Please select course path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtDBPath.Text))
            {
                MessageBox.Show("Please select database path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }
            else if (string.IsNullOrWhiteSpace(txtOutputPath.Text))
            {
                MessageBox.Show("Please select output path", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }


            foreach (ListViewItem item in list)
            {
                CourseItem courseItem = listCourse.Where(r => r.Course.Name == item.Name).Select(r => r).FirstOrDefault();

                if (chkDecrypt.Checked)
                {
                    bgwDecrypt.ReportProgress(1, new Log {
                        Text = $"Start to decrypt course \"{courseItem.Course.Title}\"", TextColor = Color.Magenta, NewLine = true, IsError = true
                    });

                    //Create new course path with the output path
                    var newCoursePath = Path.Combine(txtOutputPath.Text, this.CleanName(courseItem.Course.Title));

                    DirectoryInfo courseInfo = Directory.Exists(newCoursePath)
                        ? new DirectoryInfo(newCoursePath)
                        : Directory.CreateDirectory(newCoursePath);

                    if (chkCopyImage.Checked && File.Exists($"{courseItem.CoursePath}\\image.jpg"))
                    {
                        File.Copy($"{courseItem.CoursePath}\\image.jpg", $"{newCoursePath}\\image.jpg", true);
                    }


                    //Get list all modules in current course
                    List <Module> listModules = courseItem.Course.Modules;

                    if (listModules.Count > 0)
                    {
                        // integer to add 1 if index should start at 1
                        int startAt1 = Convert.ToInt16(chkStartModuleIndexAt1.Checked);
                        //Get each module
                        foreach (Module module in listModules)
                        {
                            //Generate module hash name
                            string moduleHash = this.ModuleHash(module.Name, module.AuthorHandle);
                            //Generate module path
                            string moduleHashPath = Path.Combine(courseItem.CoursePath, moduleHash);
                            //Create new module path with decryption name
                            string newModulePath = Path.Combine(courseInfo.FullName, $"{(startAt1 + module.Index):00}. {module.Title}");

                            if (Directory.Exists(moduleHashPath))
                            {
                                DirectoryInfo moduleInfo = Directory.Exists(newModulePath)
                                    ? new DirectoryInfo(newModulePath)
                                    : Directory.CreateDirectory(newModulePath);
                                //Decrypt all videos in current module folder
                                this.DecryptAllVideos(moduleHashPath, module, moduleInfo.FullName);
                            }
                            else
                            {
                                bgwDecrypt.ReportProgress(1, new Log {
                                    Text = $"Folder {moduleHash} not found in the current course path", TextColor = Color.Red, NewLine = true, IsError = true
                                });
                            }
                        }
                    }
                    bgwDecrypt.ReportProgress(1, new Log {
                        Text = $"Decrypt \"{courseItem.Course.Title}\" complete!", TextColor = Color.Magenta, NewLine = true, IsError = true
                    });
                }

                if (chkDelete.Checked)
                {
                    try
                    {
                        Directory.Delete(courseItem.CoursePath, true);
                    }
                    catch (Exception ex)
                    {
                        bgwDecrypt.ReportProgress(1, new Log {
                            Text = $"Delete folder course {courseItem.Course.Title} fail\n{ex.Message}", TextColor = Color.Gray, NewLine = true, IsError = true
                        });
                    }

                    try
                    {
                        RemoveCourseInDb(courseItem.CoursePath);
                    }
                    catch (Exception ex)
                    {
                        bgwDecrypt.ReportProgress(1, new Log {
                            Text = $"Delete course {courseItem.Course.Title} from db fail\n{ex.Message}", TextColor = Color.Gray, NewLine = true, IsError = true
                        });
                    }


                    bgwDecrypt.ReportProgress(1, new Log {
                        Text = $"Delete course {courseItem.Course.Title} success!", TextColor = Color.Magenta, NewLine = true
                    });
                }
            }

            bgwDecrypt.ReportProgress(100);
        }