コード例 #1
0
	private static extern int WNetAddConnection2A(ref structNetResource pstNetRes, string psPassword, string psUsername, int piFlags);
コード例 #2
0
ファイル: NetworkDrive.cs プロジェクト: petredimov/Intrensic
		// Map network drive
		private void zMapDrive(string psUsername, string psPassword)
		{
            ls_ShareName = ls_ShareName.TrimEnd('\\');

            try
            {
                string ipadress = "";
                string hostname = "";

                try
                {
                    if (ls_ShareName.Split('\\').Length > 2)
                    {
                        hostname = ls_ShareName.Split('\\')[2];
                        if (!String.IsNullOrEmpty(hostname))
                        {
                            IPAddress address = ResolveHostNameToIP(hostname);
                            if (address != null)
                            {
                                ipadress = address.ToString();
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    _logger.Error(ex.Message);
                }

                if (!string.IsNullOrEmpty(ipadress))
                {
                    ls_ShareName = ls_ShareName.Replace(hostname, ipadress);
                }

                int retries = 5;
                int cnt = 0;
                bool returnValue = false;
                string errorStr = string.Empty;
                string outPath = string.Empty;
                string notExistedPathPart = string.Empty;
                //create struct data
                structNetResource stNetRes = new structNetResource();
                stNetRes.iScope = 2;
                stNetRes.iType = RESOURCETYPE_DISK;
                stNetRes.iDisplayType = 3;
                stNetRes.iUsage = 1;
                stNetRes.sRemoteName = ls_ShareName;
                stNetRes.sLocalName = ls_Drive;

                //prepare params
                int iFlags = 0;
                if (lf_SaveCredentials) { iFlags += CONNECT_CMD_SAVECRED; }
                if (lf_Persistent) { iFlags += CONNECT_UPDATE_PROFILE; }
                if (ls_PromptForCredentials) { iFlags += CONNECT_INTERACTIVE + CONNECT_PROMPT; }
                if (psUsername == "") { psUsername = null; }
                if (psPassword == "") { psPassword = null; }
                //if force, unmap ready for new connection
                if (lf_Force) { try { zUnMapDrive(true); } catch { } }
                //call and return
                while ((!returnValue) && (cnt < retries))
                {
                    errorStr = string.Empty;
                    cnt++;
                    int res = WNetAddConnection2A(ref stNetRes, psPassword, psUsername, iFlags);
                    _logger.Info("Result from mapping:" + res);
                    if (res > (int)NetworkError.EROOR_SUCCESS)
                    {
                        if (res == (int)NetworkError.ERROR_BAD_NETPATH)
                        {
                            // possible reason non existed part of the path, try to remove a last folder name.
                            int lastIndex = ls_ShareName.LastIndexOf(@"\");
                            if (lastIndex > 2)
                            {
                                notExistedPathPart = ls_ShareName.Substring(lastIndex, ls_ShareName.Length - lastIndex) + notExistedPathPart;
                                ls_ShareName = ls_ShareName.Substring(0, lastIndex);
                                stNetRes.sRemoteName = ls_ShareName;
                                _logger.Warn("ERROR_BAD_NETPATH");
                            }
                            continue;
                        }
                        else if (res == (int)NetworkError.MULTIPLE_CONNECTIONS_TO_A_SERVER_OR_SHARED_RESOURCE_BY_THE_SAME_USER)
                        {
                            _logger.Warn("MULTIPLE_CONNECTIONS_TO_A_SERVER_OR_SHARED_RESOURCE_BY_THE_SAME_USER");
                            // Drive exist find it and just use it
                            foreach (DriveInfo drive in DriveInfo.GetDrives())
                            {
                                if (drive.GetType().ToString() == "Network" && GetUNCPath(drive.Name) == GetUNCPath(ls_ShareName))
                                {
                                    outPath = string.Format(@"{0}:\{1}", ls_Drive, notExistedPathPart.TrimStart('\\'));
                                    returnValue = true;
                                    break;
                                }
                            }
                        }
                        //errorStr = "MapDrive error:" + res.ToString();
                        Thread.Sleep(1000);
                    }
                    else
                    {
                        _logger.Info("The path is mapped.");
                        outPath = string.Format(@"{0}:\{1}", ls_Drive, notExistedPathPart.TrimStart('\\'));
                        returnValue = true;
                    }
                }

            }
            catch (Exception ex)
            {
                _logger.Error("Failed to map: " + ls_ShareName);
            }

			
			

			//if (res > 0) { throw new System.ComponentModel.Win32Exception(i); }
		}