Example #1
0
        public void DefineDosDevice()
        {
            Console.WriteLine("Volume.DefineDosDevice()");

            if (!IsAdmin())
            {
                Assert.Fail();
            }

            #region Regular Drive Mapping

            string drive = string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}", DriveInfo.GetFreeDriveLetter(), Path.VolumeSeparatorChar, Path.DirectorySeparatorChar);
            StopWatcher(true);

            // Create Regular drive mapping.
            bool actionIsTrue = false;
            try
            {
                Volume.DefineDosDevice(drive, TempFolder);
                actionIsTrue = true;
            }
            catch
            {
            }

            Console.WriteLine("\nCreated Regular drive mapping (Should be True): [{0}]\nDrive letter: [{1}] now points to: [{2}]\n\t{3}", actionIsTrue, drive, TempFolder, Reporter(true));
            Assert.IsTrue(actionIsTrue, "Regular drive mapping should have been created.");

            DriveInfo           di      = new DriveInfo(drive);
            System.IO.DriveInfo diSysIo = new System.IO.DriveInfo(drive);

            try
            {
                Assert.IsTrue(Dump(di, -21));

                // A Regular drive mapping that should be visible immediately in Explorer.
                // Seems to be invisible in Explorer on Windows 8, but visibile in an elevated cmd shell.
                //Assert.IsTrue(Directory.Exists(_driveLetter), "Drive letter not visible.");

                Console.WriteLine("\nDrive Letter: [{0}]\tGetVolumeDevice(): [{1}]", drive, Volume.GetVolumeDeviceName(drive));
                Assert.AreEqual(diSysIo.IsReady, di.IsReady);
            }
            finally
            {
                StopWatcher(true);

                // Remove Regular drive mapping.
                actionIsTrue = false;
                try
                {
                    Volume.DeleteDosDevice(drive);
                    actionIsTrue = true;
                }
                catch
                {
                }

                Console.WriteLine("\n\nRemoved Regular drive mapping (Should be True): [{0}]\nDrive letter: [{1}] has been set free.\n\t{2}\n", actionIsTrue, drive, Reporter(true));
                Assert.IsTrue(actionIsTrue, "Regular drive mapping should have been removed.");
                Assert.IsFalse(Directory.Exists(drive), "Drive letter should not be visible.");
            }

            #endregion // Regular Drive Mapping

            #region Symbolic Link Drive Mapping

            try
            {
                drive = string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}", DriveInfo.GetFreeDriveLetter(true), Path.VolumeSeparatorChar, Path.DirectorySeparatorChar);
                StopWatcher(true);

                // Create Symbolic Link.
                bool createSymbolicLink = false;
                try
                {
                    Volume.DefineDosDevice(drive, TempFolder, DosDeviceAttributes.RawTargetPath);
                    createSymbolicLink = true;
                }
                catch
                {
                }

                Console.WriteLine("\n\nCreated Symbolic link mapping (Should be True): [{0}]\nDrive letter: [{1}] now points to: [{2}]\n\t{3}", createSymbolicLink, drive, TempFolder, Reporter(true));
                Assert.IsTrue(createSymbolicLink);

                di = new DriveInfo(drive);
                Assert.IsTrue(Dump(di, -21));

                // The link is created in the NT Device Name namespace and thus not visibile in Explorer.

                // Remove Symbolic Link, no exact match: fail.
                StopWatcher(true);

                bool removeSymbolicLink = false;
                try
                {
                    Volume.DeleteDosDevice(drive, "NonExistingFolder", true);
                    removeSymbolicLink = true;
                }
                catch
                {
                }

                Console.WriteLine("\n\nRemoved Symbolic link mapping (Should be False): [{0}]\nDrive letter: [{1}] has NOT been set free.\tNo exactMatch MS-DOS device name found.\n\t{2}", removeSymbolicLink, drive, Reporter(true));
                Assert.IsFalse(removeSymbolicLink);
            }
            finally
            {
                StopWatcher(true);

                // Remove Symbolic Link, exact match: success.
                bool removeSymbolicLink = false;
                try
                {
                    Volume.DeleteDosDevice(drive, TempFolder, true);
                    removeSymbolicLink = true;
                }
                catch
                {
                }

                Console.WriteLine("\n\nRemoved Symbolic link mapping (Should be True): [{0}]\nDrive letter: [{1}] has been set free.\tFound exactMatch MS-DOS device name.\n\t{2}", removeSymbolicLink, drive, Reporter(true));
                Assert.IsTrue(removeSymbolicLink);
                Assert.IsFalse(Directory.Exists(drive));
            }

            #endregion // Symbolic Link Drive Mapping
        }