Advertisement
skornyakov

DvConnectionSettings

Apr 7th, 2016
69
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.28 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using Microsoft.Win32;
  7.  
  8. namespace TrainingTaskThree
  9. {
  10.     public class DvConnectionSettings
  11.     {
  12.         private string ServerName { get; }
  13.         private string BaseName { get; }
  14.         private string UserName { get; }
  15.         private string Password { get; }
  16.         public DvConnectionSettings(string serverName, string baseName, string userName, string password)
  17.         {
  18.             ServerName = FixServerUrl(string.IsNullOrEmpty(serverName) ? GetClientServerUrl() : serverName);
  19.             BaseName = baseName;
  20.             UserName = userName;
  21.             Password = password;
  22.         }
  23.  
  24.         public DvConnectionSettings()
  25.             : this(GetClientServerUrl(), string.Empty, string.Empty, string.Empty)
  26.         {
  27.         }
  28.  
  29.         public string ConnectionString =>
  30.             $"ConnectAddress={ServerName}StorageServer/StorageServerService.asmx;BaseName={BaseName};UserName={UserName};Password={Password}";
  31.  
  32.         private string FixServerUrl(string url)
  33.         {
  34.             if (!url.StartsWith("http://")) url = "http://" + url;
  35.             if (!url.EndsWith("/")) url += "/";
  36.             if (!url.ToLower().EndsWith("docsvision/")) url += "docsvision/";
  37.             return url;
  38.         }
  39.  
  40.         private static string GetClientServerUrl()
  41.         {
  42.             var name = $"SOFTWARE\\DocsVision\\Platform\\{"5.0"}\\Client";
  43.             var registryKey = Registry.CurrentUser.OpenSubKey(name);
  44.             if (registryKey == null)
  45.             {
  46.                 registryKey = Registry.LocalMachine.OpenSubKey(name);
  47.                 if (registryKey == null)
  48.                 {
  49.                     name = $"SOFTWARE\\Wow6432Node\\DocsVision\\Platform\\{"5.0"}\\Client";
  50.                     registryKey = Registry.CurrentUser.OpenSubKey(name) ?? Registry.LocalMachine.OpenSubKey(name);
  51.                 }
  52.             }
  53.             if (registryKey == null) throw new ArgumentException("Server Url settings not found");
  54.             var url = (string)registryKey.GetValue("ServerUrl", string.Empty);
  55.             if (string.IsNullOrEmpty(url)) throw new ArgumentException("Server Url settings not found");
  56.             return url;
  57.         }
  58.  
  59.     }
  60. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement