Beispiel #1
0
        //TODO check for partition

        /// <summary>
        ///
        /// </summary>
        /// <param name="pnpid"></param>
        /// <exception cref="ArgumentNullException"></exception>
        public Automountusb MountUsb(string pnpid)
        {
            if (string.IsNullOrEmpty(pnpid))
            {
                throw new ArgumentNullException("pnpid");
            }

            // Get Singelton for config
            _config = Singleton <ConfigManager> .Instance.Init(_config);

            _language = _config.GetValue(ConfigTrm.Mainconfig.Section, ConfigTrm.Mainconfig.Language, "");
            ValidateMount vm = new ValidateMount();

            pnpid = pnpid.Replace(@"\\", @"\");
            string[]       stringindex = { "USBSTOR", "SCSI\\DISK" };
            MountVareables mvd;

            State = false;
            int start;

            foreach (string sindex in stringindex)
            {
                if (pnpid.Contains(sindex))
                {
                    try
                    {
                        start = pnpid.IndexOf(sindex);
                        pnpid = pnpid.Substring(start, pnpid.Length - start - 1);
                    }
                    catch (Exception)
                    {
                        pnpid = "";
                    }
                }
            }



            string[] sections    = _config.GetSectionNames();
            string   configPnPid = "";

            foreach (string section in sections)
            {
                Name = section;
                string configtype = _config.GetValue(section, ConfigTrm.Drive.Type, "");

                if (configtype.Equals("Container"))
                {
                    configPnPid = _config.GetValue(section, ConfigTrm.Container.Pnpid, "");
                    if (configPnPid.Equals(pnpid))
#if DEBUG
                    { MessageBox.Show(pnpid + "  " + configPnPid); }
#endif
                    {
                        if (_config.GetValue(section, ConfigTrm.Container.Automountusb, false))
                        {
                            try
                            {
                                mvd = vm.ValidateMountContainer(section, _language);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, LanguagePool.GetInstance().GetString(LanguageRegion, "Error", _language), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return(this);
                            }

                            int ret = Mount.MountContainer(mvd.path, mvd.driveletter, mvd.key, mvd.password, mvd.silent, mvd.beep, mvd.force, mvd.readOnly, mvd.removalbe, mvd.tc, mvd.pim, mvd.hash);

                            if (ret == 0)
                            {
                                State = true;
                            }
                            return(this);
                        }
                    }
                }


                if (configtype.Equals("Drive"))
                {
                    configPnPid = _config.GetValue(section, ConfigTrm.Drive.Pnpdeviceid, "");

                    if (configPnPid.Equals(pnpid))
                    {
#if DEBUG
                        MessageBox.Show(pnpid + "  " + configPnPid);
#endif
                        if (_config.GetValue(section, ConfigTrm.Drive.Automountusb, false))
                        {
                            try
                            {
                                mvd = vm.ValidateMountDrive(section, _language);
                            }
                            catch (Exception ex)
                            {
                                MessageBox.Show(ex.Message, LanguagePool.GetInstance().GetString(LanguageRegion, "Error", _language), MessageBoxButtons.OK, MessageBoxIcon.Warning);
                                return(this);
                            }

                            int res = Mount.MountDrive(mvd.partitionlist, mvd.driveletter, mvd.key, mvd.password, mvd.silent, mvd.beep, mvd.force, mvd.readOnly, mvd.removalbe, mvd.pim, mvd.hash, mvd.tc);

                            if (res == 0)
                            {
                                State = true;
                            }

                            return(this);
                        }
                    }
                }
            }
            return(this);
        }
        /// <summary>
        /// Mount all containers and drives witch automountstart flag
        /// </summary>
        public void StartMount()
        {
            string         _language = _config.GetValue(ConfigTrm.Mainconfig.Section, ConfigTrm.Mainconfig.Language, "");
            ValidateMount  vm        = new ValidateMount();
            MountVareables mvd       = new MountVareables();
            List <string>  containers;
            List <string>  drives;

            containers = GetAutoContainers();
            drives     = GetAutoDrives();
            bool error = false;
            int  ret;
            int  res;

            foreach (string name in containers)
            {
                error = false;
                try
                {
                    mvd = vm.ValidateMountContainer(name, _language);
                }
                catch (DrivletterUsedException dx)
                {
                    //Dont show error if drivletter is used. Drive is probalbly mounted
                    error = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, LanguagePool.GetInstance().GetString(LanguageRegion, "Error", _language), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    error = true;
                }
                if (!error)
                {
                    try
                    {
                        ret = Mount.MountContainer(mvd.path, mvd.driveletter, mvd.key, mvd.password, mvd.silent, mvd.beep, mvd.force, mvd.readOnly, mvd.removalbe, mvd.tc, mvd.pim, mvd.hash);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, LanguagePool.GetInstance().GetString(LanguageRegion, "Error", _language), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }

            foreach (string name in drives)
            {
                error = false;
                try
                {
                    mvd = vm.ValidateMountDrive(name, _language);
                }
                catch (DrivletterUsedException dx)
                {
                    //Dont show error if drivletter is used. Drive is probalbly mounted
                    error = true;
                }
                catch (Exception ex)
                {
                    MessageBox.Show(ex.Message, LanguagePool.GetInstance().GetString(LanguageRegion, "Error", _language), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    error = true;
                }
                if (!error)
                {
                    try
                    {
                        res = Mount.MountDrive(mvd.partitionlist, mvd.driveletter, mvd.key, mvd.password, mvd.silent, mvd.beep, mvd.force, mvd.readOnly, mvd.removalbe, mvd.pim, mvd.hash, mvd.tc);
                    }
                    catch (Exception ex)
                    {
                        MessageBox.Show(ex.Message, LanguagePool.GetInstance().GetString(LanguageRegion, "Error", _language), MessageBoxButtons.OK, MessageBoxIcon.Error);
                    }
                }
            }
        }