コード例 #1
0
ファイル: AlphaFS_HostTest.cs プロジェクト: CDEApp/CDE
        public void ConnectDrive()
        {
            Console.WriteLine("Network.Host.ConnectDrive()");

            #region Connect drive to share

            string drive = string.Format(CultureInfo.CurrentCulture, "{0}{1}{2}", DriveInfo.GetFreeDriveLetter(), Path.VolumeSeparatorChar, Path.DirectorySeparatorChar);
            string share = Path.LocalToUnc(LocalHostShare);
            bool   connectOk;
            Console.WriteLine("\nConnect using a designated drive: [{0}]", drive);
            try
            {
                StopWatcher(true);
                Host.ConnectDrive(drive, share);
                Console.WriteLine("\nConnectDrive(): [{0}] to: [{1}]\n\n\t{2}\n", drive, share, Reporter(true));

                connectOk = true;
            }
            catch (Exception ex)
            {
                connectOk = false;
                Console.WriteLine("\nFailed ConnectDrive(): [{0}] to: [{1}]", drive, share);
                Console.WriteLine("\nCaught Exception: [{0}]", ex.Message.Replace(Environment.NewLine, "  "));
            }

            Assert.IsTrue(connectOk);

            #endregion // Connect drive to share

            #region Disconnect drive from share

            bool disconnectOk = false;

            // We only need this for the unit test.
            while (!disconnectOk)
            {
                try
                {
                    StopWatcher(true);
                    Host.DisconnectDrive(drive);
                    Console.WriteLine("\nDisconnectDrive(): [{0}] from: [{1}]\n\n\t{2}\n", drive, share, Reporter(true));

                    disconnectOk = true;
                }
                catch (Exception ex)
                {
                    disconnectOk = false;
                    Console.WriteLine("\nFailed DisconnectDrive(): [{0}] from: [{1}]", drive, share);
                    Console.WriteLine("\nCaught Exception: [{0}]", ex.Message.Replace(Environment.NewLine, "  "));
                }
            }

            Assert.IsTrue(disconnectOk);

            #endregion // Disconnect  drive from share


            #region Connect last available drive to share

            Console.WriteLine("\nConnect using the last available drive.");
            drive = null;
            try
            {
                StopWatcher(true);
                drive = Host.ConnectDrive(null, share);
                Console.WriteLine("\nConnectDrive(): [{0}] to: [{1}]\n\n\t{2}\n", drive, share, Reporter(true));

                connectOk = true;
            }
            catch (Exception ex)
            {
                connectOk = false;
                Console.WriteLine("\nFailed ConnectDrive(): [{0}] to: [{1}]", drive, share);
                Console.WriteLine("\nCaught Exception: [{0}]", ex.Message.Replace(Environment.NewLine, "  "));
            }

            Assert.IsTrue(connectOk);

            #endregion // Connect last available drive to share

            #region Disconnect last available drive from share

            disconnectOk = false;

            // We only need this for the unit test.
            while (!disconnectOk)
            {
                try
                {
                    StopWatcher(true);
                    Host.DisconnectDrive(drive);
                    Console.WriteLine("\nDisconnectDrive(): [{0}] from: [{1}]\n\n\t{2}\n", drive, share, Reporter(true));

                    disconnectOk = true;
                }
                catch (Exception ex)
                {
                    disconnectOk = false;
                    Console.WriteLine("\nFailed DisconnectDrive(): [{0}] from: [{1}]", drive, share);
                    Console.WriteLine("\nCaught Exception: [{0}]", ex.Message.Replace(Environment.NewLine, "  "));
                }
            }

            Assert.IsTrue(disconnectOk);

            #endregion // Disconnect last available drive from share
        }