Пример #1
0
        /// <summary>
        ///
        /// </summary>
        /// <param name="path"></param>
        /// <param name="username"></param>
        /// <param name="password"></param>
        /// <returns></returns>
        public static ResultOperation ConnectSharedContent(string path, string username, string password)
        {
            ResultOperation retorno = new ResultOperation();

            retorno.ProcessedOK = true;

            try
            {
                if (!string.IsNullOrEmpty(path) && path.Trim().StartsWith(@"\\") && path.Trim().Length >= 3)
                {
                    // Verificando se o mapeamento já existe, pois se já existir não precisará mais proceder com o mapeamento
                    DirectoryInfo rootFolder = new DirectoryInfo(path);
                    if (!CommonInternal.IsAccessableFolder(rootFolder))
                    {
                        // Utilizando a API de acesso a pastas de rede
                        ConnectToRemoteInternal(path, username, password);

                        string[] folders      = path.Split(new string[] { @"\" }, StringSplitOptions.RemoveEmptyEntries);
                        string   subRemoteUNC = string.Empty;
                        for (int i = 0; i < (folders.Length - 1); i++)
                        {
                            subRemoteUNC = subRemoteUNC + folders[i] + @"\";
                        }
                        if (!string.IsNullOrEmpty(subRemoteUNC))
                        {
                            ConnectSharedContent(@"\\" + subRemoteUNC, username, password);
                        }
                    }
                }
                else
                {
                    retorno.ProcessedOK = false;
                    retorno.Message     = "Folder selected isn't valid network path.";
                }
            }
            catch (Exception ex)
            {
                retorno.ProcessedOK = false;
                retorno.Exception   = ex;
                retorno.Message     = ex.Message;
            }

            return(retorno);
        }
        /// <summary>
        /// Static method with main parameters
        /// </summary>
        /// <param name="unitDrive">Sample: X:</param>
        /// <param name="pathNetwork">Sample: \\myserver</param>
        /// <param name="username">Sample: domain\billgates</param>
        /// <param name="password"></param>
        public static ResultOperation MapDrive(string unitDrive, string pathNetwork, string username, string password)
        {
            ResultOperation result = new ResultOperation();

            result.ProcessedOK = true;

            try
            {
                // Verificando se os parâmetros de mapeamento foram mencionados no Web.config
                if (!string.IsNullOrEmpty(unitDrive) && !string.IsNullOrEmpty(pathNetwork)) // && !string.IsNullOrEmpty(username) && !string.IsNullOrEmpty(password))
                {
                    // Verificando se o mapeamento já existe, pois se já existir não precisará mais proceder com o mapeamento
                    DirectoryInfo rootFolder = new DirectoryInfo(unitDrive + (unitDrive.EndsWith(":") ? "" : ":") + @"\");
                    if (!CommonInternal.IsAccessableFolder(rootFolder))
                    {
                        // Utilizando a API de mapeamento
                        SharedContentMapping networkExtendedDrive = new SharedContentMapping();
                        networkExtendedDrive.Force                = true;
                        networkExtendedDrive.Persistent           = true;
                        networkExtendedDrive.LocalDrive           = unitDrive + (unitDrive.EndsWith(":") ? "" : ":");
                        networkExtendedDrive.PromptForCredentials = false;
                        networkExtendedDrive.ShareName            = pathNetwork;
                        networkExtendedDrive.SaveCredentials      = true;
                        networkExtendedDrive.MapDrive(username, password);
                        networkExtendedDrive = null;
                    }
                }
                else
                {
                    result.ProcessedOK = false;
                    result.Message     = "Drive destination and network path are necessary for mapping drive.";
                }
            }
            catch (Exception ex)
            {
                result.ProcessedOK = false;
                result.Exception   = ex;
            }

            return(result);
        }