Removable Drive Detection helper class
示例#1
0
        public static bool Mount(string IsoFile, out string VirtualDrive)
        {
            if (g_Player.Playing)
            {
                //string file = g_Player.CurrentFile;
                //if (g_Player.CheckIfImage(file))
                {
                    g_Player.Stop();
                }
            }
            VirtualDrive = string.Empty;
            if (IsoFile == null)
            {
                return(false);
            }
            if (IsoFile == string.Empty)
            {
                return(false);
            }
            if (!_Enabled)
            {
                return(false);
            }
            if (!System.IO.File.Exists(_Path))
            {
                return(false);
            }
            DateTime startTime = DateTime.Now;

            System.IO.DriveInfo drive = new System.IO.DriveInfo(_Drive);
            UnMount();

            IsoFile = Utils.RemoveTrailingSlash(IsoFile);
            string strParams;

            if (!_DriveType.Equals(VirtualCloneDrive))
            {
                strParams = String.Format("-mount {0}, {1},\"{2}\"", _DriveType, _DriveNo, IsoFile);
            }
            else
            {
                strParams = String.Format("-mount {0},\"{1}\"", _DriveNo, IsoFile);
            }
            Process p       = Utils.StartProcess(_Path, strParams, true, true);
            int     timeout = 0;

            while ((!p.HasExited || !drive.IsReady || !System.IO.Directory.Exists(_Drive + @"\")) && (timeout < 10000))
            {
                System.Threading.Thread.Sleep(100);
                timeout += 100;
            }
            if (timeout >= 10000)
            {
                GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_ASKYESNO, 0, 0, 0, 0, 0, 0);
                msg.Param1  = 200093;
                msg.Param2  = 200094;
                msg.Param3  = 0;
                msg.Param4  = 0;
                msg.Object2 = true;
                GUIWindowManager.SendMessage(msg);
                if (msg.Param1 == 1)
                {
                    while ((!p.HasExited || !drive.IsReady || !System.IO.Directory.Exists(_Drive + @"\")) && (timeout < 60000))
                    {
                        System.Threading.Thread.Sleep(100);
                        timeout += 100;
                    }
                    if (timeout >= 60000)
                    {
                        Log.Error("Mounting failed after {0}s (second timeout). Check your settings.", (int)(timeout / 1000));
                        UnMount();
                        return(false);
                    }
                }
                else
                {
                    Log.Error("Mounting failed after {0}s (first timeout). Check your settings.", (int)(timeout / 1000));
                    UnMount();
                    return(false);
                }
            }
            RemovableDriveHelper.SetMountTime(DateTime.Now);
            VirtualDrive    = _Drive;
            _MountedIsoFile = IsoFile;
            Log.Debug("Mount time: {0}s", String.Format("{0:N}", (DateTime.Now - startTime).TotalSeconds));
            return(true);
        }
        public static bool Mount(string IsoFile, out string VirtualDrive)
        {
            if (g_Player.Playing)
            {
                //string file = g_Player.CurrentFile;
                //if (g_Player.CheckIfImage(file))
                {
                    g_Player.Stop();
                }
            }
            VirtualDrive = string.Empty;
            if (IsoFile == null)
            {
                return(false);
            }
            if (IsoFile == string.Empty)
            {
                return(false);
            }
            if (!_Enabled)
            {
                return(false);
            }
            if (!_DriveType.Equals("native") && !System.IO.File.Exists(_Path))
            {
                return(false);
            }
            DateTime startTime = DateTime.Now;

            UnMount();

            IsoFile = Utils.RemoveTrailingSlash(IsoFile);
            string strParams;

            System.IO.DriveInfo drive;
            int timeout = 0;

            if (_DriveType.Equals("native"))
            {
                if (!string.IsNullOrEmpty(IsoFile))
                {
                    if (_DriveType.Equals("native"))
                    {
                        using (var ps = PowerShell.Create())
                        {
                            Log.Debug("Dismount-DiskImage {0}", IsoFile);
                            ps.AddCommand("Dismount-DiskImage").AddParameter("ImagePath", IsoFile).Invoke();

                            while (System.IO.Directory.Exists(_Drive + @"\") && (timeout < 10000))
                            {
                                System.Threading.Thread.Sleep(100);
                                timeout += 100;
                            }
                        }
                    }
                }

                using (var ps = PowerShell.Create())
                {
                    // Set mounted ISO file to be able to unmount it if something failed to load.
                    _MountedIsoFile = IsoFile;
                    Log.Debug("Mount-DiskImage {0}", IsoFile);
                    ps.AddCommand("Mount-DiskImage").AddParameter("ImagePath", IsoFile).AddParameter("PassThru");
                    var psResult = ps.Invoke();
                    Log.Debug("Mount-DiskImage Result {0}", psResult.Count);
                }

                using (var ps = PowerShell.Create())
                {
                    ps.AddCommand("Get-DiskImage").AddParameter("ImagePath", IsoFile).AddCommand("Get-Volume");
                    var psResult = ps.Invoke();
                    Log.Debug("Mount-get drive letter Result {0}", psResult.Count);
                    foreach (var driveLetter in psResult.Select(result => result.Members["DriveLetter"].Value.ToString()))
                    {
                        _Drive = String.Format("{0}:", driveLetter);
                        Log.Debug("Mount-DiskImage DriveLetter {0}", _Drive);
                    }
                }

                drive = new System.IO.DriveInfo(_Drive);

                while ((!drive.IsReady || !System.IO.Directory.Exists(_Drive + @"\")) && (timeout < 10000))
                {
                    System.Threading.Thread.Sleep(100);
                    timeout += 100;
                }
            }
            else
            {
                if (!_DriveType.Equals(VirtualCloneDrive))
                {
                    strParams = String.Format("-mount {0}, {1},\"{2}\"", _DriveType, _DriveNo, IsoFile);
                }
                else
                {
                    strParams = String.Format("-mount {0},\"{1}\"", _DriveNo, IsoFile);
                }
                Process p = Utils.StartProcess(_Path, strParams, true, true);

                drive = new System.IO.DriveInfo(_Drive);

                while ((!p.HasExited || !drive.IsReady || !System.IO.Directory.Exists(_Drive + @"\")) && (timeout < 10000))
                {
                    System.Threading.Thread.Sleep(100);
                    timeout += 100;
                }
            }

            if (timeout >= 10000)
            {
                GUIMessage msg = new GUIMessage(GUIMessage.MessageType.GUI_MSG_ASKYESNO, 0, 0, 0, 0, 0, 0);
                msg.Param1  = 200093;
                msg.Param2  = 200094;
                msg.Param3  = 0;
                msg.Param4  = 0;
                msg.Object2 = true;
                GUIWindowManager.SendMessage(msg);
                if (msg.Param1 == 1)
                {
                    while ((!drive.IsReady || !System.IO.Directory.Exists(_Drive + @"\")) && (timeout < 60000))
                    {
                        System.Threading.Thread.Sleep(100);
                        timeout += 100;
                    }
                    if (timeout >= 60000)
                    {
                        Log.Error("Mounting failed after {0}s (second timeout). Check your settings.", (int)(timeout / 1000));
                        UnMount();
                        return(false);
                    }
                }
                else
                {
                    Log.Error("Mounting failed after {0}s (first timeout). Check your settings.", (int)(timeout / 1000));
                    UnMount();
                    return(false);
                }
            }
            RemovableDriveHelper.SetMountTime(DateTime.Now);
            VirtualDrive    = _Drive;
            _MountedIsoFile = IsoFile;
            Log.Debug("Mount time: {0}s", String.Format("{0:N}", (DateTime.Now - startTime).TotalSeconds));
            return(true);
        }