コード例 #1
0
        private void MountNetworkDrive(Models.NetworkCredential cred)
        {
            string message = null;

            try
            {
                DriveInfo drive = GetDriveInfo(cred.MountPoint);
                if (drive != null)
                {
                    string userName       = null;
                    string remotePath     = MappedDriveResolver.ResolveToRootUNC(cred.MountPoint);
                    string credentialUsed = MappedDriveResolver.GetCredentialUsedToMapNetworkDrive(cred.MountPoint);
                    message = string.Format("{0} is already mounted to {1} by {2}", cred.MountPoint, remotePath, credentialUsed);
                    logger.Info(message);

                    // Was this mounted using the same credential?
                    // We assume the mount point is OK if we cannot retrieve the username that originally mounted it.
                    if (userName != null && (userName.Equals(cred.Login, StringComparison.InvariantCulture) ||
                                             userName.Equals(MappedDriveResolver.UNKNOWN_CREDENTIAL, StringComparison.InvariantCulture)))
                    {
                        return;                         // We're OK then.
                    }

                    NetworkDriveMapper.UnmountNetworkLocation(cred.MountPoint);
                    logger.Info("Umounted {0}", cred.MountPoint);
                }

                NetworkDriveMapper.MountNetworkLocation(cred.MountPoint, cred.Path, cred.Login, cred.Password, false);
                message = string.Format("Successfully mounted {0} to {1} as {2}", cred.MountPoint, cred.Path, cred.Login);
                logger.Info(message);
            }
            catch (Win32Exception ex)
            {
                string reason = ex.Message;

                switch (ex.NativeErrorCode)
                {
                case NetworkDriveMapper.ERROR_ALREADY_ASSIGNED:
                {
                    //string userName = null;
                    string remotePath     = MappedDriveResolver.ResolveToRootUNC(cred.MountPoint);
                    string credentialUsed = MappedDriveResolver.GetCredentialUsedToMapNetworkDrive(cred.MountPoint);
                    reason = string.Format("It's already mounted to {0} by {1}", remotePath, credentialUsed);
                    break;
                }
                }

                message = string.Format("Failed to mount {0} to {1} as {2} - {3}", cred.MountPoint, cred.Path, cred.Login, reason);
                throw new FailedToMountNetworkDrive(message);
            }
        }
コード例 #2
0
ファイル: FormMainSlave.cs プロジェクト: MOGwareSupport/MOG
        public string RefreshConnectionToolText()
        {
            // get SQL server and MOG Repository drive mapping info
            string connectionString = MOG_ControllerSystem.GetDB().GetConnectionString();
            string sqlServerName    = "NONE";

            if (connectionString.ToLower().IndexOf("data source") != -1)
            {
                // parse out sql server name
                //sqlServerName = connectionString.Substring(connectionString.ToLower().IndexOf("data source=")+13,
                string[] substrings = connectionString.Split(";".ToCharArray());

                // look for correct part o' the connection string
                foreach (string substring in substrings)
                {
                    if (substring.ToLower().StartsWith("data source="))
                    {
                        sqlServerName = substring.Substring(substring.IndexOf("=") + 1).ToUpper();
                    }
                }
            }

            try
            {
                // get MOG repository drive and mapping info
                string mogDrive       = MOG_ControllerSystem.GetSystemRepositoryPath();
                char   mogDriveLetter = Path.GetPathRoot(mogDrive)[0];
                string mogDriveTarget = new NetworkDriveMapper().GetDriveMapping(mogDriveLetter);

                // Connected to X-SERVER @IP 192.168.5.5; SQL Server: JBIANCHI; MOG Repository M: mapped to \\GX\MOG
                //ConnectionString=Packet size=4096;integrated security=SSPI;data source=NEMESIS;persist security info=False;initial catalog=mog16;

                return(string.Concat(MOG_ControllerSystem.GetServerComputerName(), " SQL SERVER: ", sqlServerName));
            }
            catch (Exception e)
            {
                MOG_Report.ReportMessage("RefreshConnectionToolText", e.Message, e.StackTrace, MOG.PROMPT.MOG_ALERT_LEVEL.CRITICAL);
            }

            return("Connected");
        }