Beispiel #1
0
        private void FrmMain_Close(object sender, FormClosingEventArgs e)
        {
            int exitCode = new int();

            if (!String.IsNullOrEmpty(_MappedDrive))
            {
                exitCode = DriveSettings.DisconnectNetworkDrive(_MappedDrive, true);
            }

            if (exitCode != 0)
            {
                MessageBox.Show("Exiting with code: " + exitCode + Environment.NewLine + Environment.NewLine + "PLEASE UNMAP DRIVE MANUALLY", "Exit Code", MessageBoxButtons.OK);
            }
        }
Beispiel #2
0
        private void CheckNetworkPathAvailability(string state)
        {
            ManagementScope          scope  = new ManagementScope("root\\CIMV2");
            SelectQuery              sQuery = new SelectQuery("Select * from Win32_LogicalDisk Where DriveType = 4");
            ManagementObjectSearcher search = new ManagementObjectSearcher(sQuery);
            char newDriveLetter             = new char();
            bool bFound = false;

            for (char c = 'Z'; c >= 'D'; c--)
            {
                bool   bNotFound = true;
                string sCheck    = c.ToString() + ":";
                foreach (ManagementObject queryObj in search.Get())
                {
                    if (sCheck == queryObj["DeviceID"].ToString())
                    {
                        bNotFound = false;
                    }
                }
                bFound = bNotFound;
                if (bFound)
                {
                    newDriveLetter = c;
                    break;
                }
            }
            if (newDriveLetter == '\0')
            {
                ErrorAndExit("No available network drive letters." + Environment.NewLine + "Please unmap drives manually", "No available letters.", "Network Drives Full");
            }
            else
            {
                // tries to connect to an alias
                ArrayList shareNames = new ArrayList();
                shareNames.Add(@"\\ZEUS\Add_Software\Log\");
                shareNames.Add(@"\\ZEUS1\Add_Software\Log\");
                shareNames.Add(@"\\ZEUS2\Add_Software\Log\");
                shareNames.Add(@"\\ZEUS3\Add_Software\Log\");
                shareNames.Add(@"\\ZEUS4\Add_Software\Log\");
                shareNames.Add(@"\\boxxintranet\engineering\TempCharlesTest\");

                string sPassword = null;
                string sUserName = null;
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["MapPassword"].ToString()))
                {
                    sPassword = ConfigurationManager.AppSettings["MapPassword"].ToString();
                }
                if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["MapUserName"].ToString()))
                {
                    sUserName = ConfigurationManager.AppSettings["MapUserName"].ToString();
                }
                DriveSettings ds = new DriveSettings();

                int returnValue = 0;
                foreach (string mapFolderPath in shareNames)
                {
                    returnValue = ds.MapNetworkDrive(newDriveLetter.ToString(), mapFolderPath, sUserName, sPassword);
                    if (returnValue == 0)
                    {
                        if (state == "loadUp")
                        {
                            lblShare.BackColor = Color.Green;
                            lblShare.ForeColor = Color.White;
                            lblShare.Text      = "Shared Folder: " + newDriveLetter.ToString() + @":\";
                        }
                        _MappedDrive = newDriveLetter.ToString() + ":";
                        //MessageBox.Show("Connected to:\t" + mapFolderPath);
                        break;
                    }
                }
                if (returnValue != 0)
                {
                    if (state == "loadUp")
                    {
                        lblShare.BackColor = Color.Red;
                    }
                    MessageBox.Show(
                        "return value:\t" + returnValue + Environment.NewLine +
                        "Mapped Drive:\t" + _MappedDrive?.ToString());
                    ErrorAndExit("Unable to connect to shared drive.", "Unable to connect to shared drive", "Shared Drive Error");
                }
            }
        }