Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- using System.Threading.Tasks;
- using Microsoft.Win32;
- namespace TrainingTaskThree
- {
- public class DvConnectionSettings
- {
- private string ServerName { get; }
- private string BaseName { get; }
- private string UserName { get; }
- private string Password { get; }
- public DvConnectionSettings(string serverName, string baseName, string userName, string password)
- {
- ServerName = FixServerUrl(string.IsNullOrEmpty(serverName) ? GetClientServerUrl() : serverName);
- BaseName = baseName;
- UserName = userName;
- Password = password;
- }
- public DvConnectionSettings()
- : this(GetClientServerUrl(), string.Empty, string.Empty, string.Empty)
- {
- }
- public string ConnectionString =>
- $"ConnectAddress={ServerName}StorageServer/StorageServerService.asmx;BaseName={BaseName};UserName={UserName};Password={Password}";
- private string FixServerUrl(string url)
- {
- if (!url.StartsWith("http://")) url = "http://" + url;
- if (!url.EndsWith("/")) url += "/";
- if (!url.ToLower().EndsWith("docsvision/")) url += "docsvision/";
- return url;
- }
- private static string GetClientServerUrl()
- {
- var name = $"SOFTWARE\\DocsVision\\Platform\\{"5.0"}\\Client";
- var registryKey = Registry.CurrentUser.OpenSubKey(name);
- if (registryKey == null)
- {
- registryKey = Registry.LocalMachine.OpenSubKey(name);
- if (registryKey == null)
- {
- name = $"SOFTWARE\\Wow6432Node\\DocsVision\\Platform\\{"5.0"}\\Client";
- registryKey = Registry.CurrentUser.OpenSubKey(name) ?? Registry.LocalMachine.OpenSubKey(name);
- }
- }
- if (registryKey == null) throw new ArgumentException("Server Url settings not found");
- var url = (string)registryKey.GetValue("ServerUrl", string.Empty);
- if (string.IsNullOrEmpty(url)) throw new ArgumentException("Server Url settings not found");
- return url;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement