139 lines
5.6 KiB
C#
139 lines
5.6 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.Configuration;
|
|
using System.Data.SqlClient;
|
|
using System.Linq;
|
|
using System.Security.Principal;
|
|
using Microsoft.Win32;
|
|
using LocalSQLDB.Properties;
|
|
|
|
namespace LocalSQLDB
|
|
{
|
|
public class LocalDBPrepare
|
|
{
|
|
public LocalDBPrepare(string targetDir, Version productVersion)
|
|
{
|
|
_targetDir = targetDir;
|
|
_installingVersion = productVersion;
|
|
}
|
|
|
|
static string _targetDir = string.Empty;
|
|
static Version _installingVersion = new Version();
|
|
|
|
private Configuration _newConfig;
|
|
private SettingElementCollection _newSettings = new SettingElementCollection();
|
|
private Dictionary<string, string> _newSettingsDictionary = new Dictionary<string, string>();
|
|
|
|
private enum DbType
|
|
{
|
|
Centralized = 0,
|
|
Local = 1
|
|
}
|
|
|
|
private enum ConfigSettings
|
|
{
|
|
DBType,
|
|
LocalDbHost,
|
|
DBName,
|
|
UseNTLMAuthentication,
|
|
LocalDBUser,
|
|
LocalDBPassword
|
|
}
|
|
|
|
public bool PrepareDB()
|
|
{
|
|
//Only attach to SqlLocalDb if DataPRO will be using a local (not centralized) database
|
|
GetNewConfig();
|
|
|
|
var dbType = DbType.Local;
|
|
try
|
|
{
|
|
DbType intDbType;
|
|
if (Enum.TryParse(_newSettingsDictionary[ConfigSettings.DBType.ToString()], out intDbType))
|
|
{
|
|
switch (intDbType)
|
|
{
|
|
case DbType.Centralized:
|
|
dbType = DbType.Centralized;
|
|
break;
|
|
case DbType.Local:
|
|
default:
|
|
dbType = DbType.Local;
|
|
break;
|
|
}
|
|
}
|
|
else
|
|
{
|
|
dbType = DbType.Local;
|
|
}
|
|
}
|
|
catch
|
|
{
|
|
dbType = DbType.Local;
|
|
}
|
|
|
|
if (dbType == DbType.Centralized)
|
|
{
|
|
return false;
|
|
}
|
|
|
|
var dbName = Settings.Default.DataPRO;
|
|
try { dbName = _newSettingsDictionary[ConfigSettings.DBName.ToString()]; }
|
|
catch { dbName = Settings.Default.DataPRO; }
|
|
|
|
if (string.Equals(dbName, Settings.Default.DataPRO, StringComparison.CurrentCultureIgnoreCase)) return true;
|
|
|
|
var dbFileName = System.IO.Path.Combine(_targetDir, Settings.Default.LocalDbFolder, dbName + Settings.Default.Mdf);
|
|
var dbFileLogName = System.IO.Path.Combine(_targetDir, Settings.Default.LocalDbFolder, dbName + Settings.Default.LogLdf);
|
|
|
|
var sourceFileName = string.Empty;
|
|
//if (_newSettingsDictionary[ConfigSettings.DBCopy.ToString()])
|
|
//{
|
|
string mostRecentlyInstalledLowerVersion;
|
|
var mostRecentlyInstalledSubKeyName = Common.PreviousInstall.GetMostRecentlyInstalledSubKeyName(_installingVersion, out mostRecentlyInstalledLowerVersion);
|
|
if (mostRecentlyInstalledSubKeyName == string.Empty) return false;
|
|
//if (_newSettingsDictionary[ConfigSettings.DBCopy.ToString()])
|
|
//{
|
|
var _previousDir = Common.PreviousInstall.GetMostRecentlyInstalledPath(mostRecentlyInstalledSubKeyName);
|
|
sourceFileName = System.IO.Path.Combine(_previousDir, Settings.Default.LocalDbFolder, Settings.Default.DataPRO + Settings.Default.Mdf);
|
|
//}
|
|
//else
|
|
//{
|
|
sourceFileName = System.IO.Path.Combine(_targetDir, Settings.Default.LocalDbFolder, Settings.Default.DataPRO + Settings.Default.Mdf);
|
|
//}
|
|
System.IO.File.Copy(sourceFileName, dbFileName);
|
|
//System.IO.File.Delete(sourceFileName);
|
|
|
|
//if (_newSettingsDictionary[ConfigSettings.DBCopy.ToString()])
|
|
//{
|
|
// sourceFileName = System.IO.Path.Combine(_previousDir, Settings.Default.LocalDbFolder, Settings.Default.DataPRO + Settings.Default.LogLdf);
|
|
//}
|
|
//else
|
|
//{
|
|
sourceFileName = System.IO.Path.Combine(_targetDir, Settings.Default.LocalDbFolder, Settings.Default.DataPRO + Settings.Default.LogLdf);
|
|
//}
|
|
System.IO.File.Copy(sourceFileName, dbFileLogName);
|
|
//System.IO.File.Delete(sourceFileName);
|
|
return true;
|
|
}
|
|
|
|
private void GetNewConfig()
|
|
{
|
|
var newPath = string.Empty;
|
|
//Open the new config file just installed
|
|
newPath = _targetDir + Settings.Default.RegistryDataPROExe;
|
|
_newConfig = ConfigurationManager.OpenExeConfiguration(@newPath);
|
|
_newSettings = GetConfigApplicationSettings();
|
|
_newSettingsDictionary = _newSettings.Cast<SettingElement>().ToDictionary(newSetting => newSetting.Name, newSetting => newSetting.Value.ValueXml.InnerXml);
|
|
}
|
|
|
|
private SettingElementCollection GetConfigApplicationSettings()
|
|
{
|
|
var configurationSectionGroup = _newConfig.SectionGroups[Settings.Default.ApplicationSettings];
|
|
if (configurationSectionGroup == null) return null;
|
|
var clientSettingsSection = configurationSectionGroup.Sections[0] as System.Configuration.ClientSettingsSection;
|
|
return clientSettingsSection != null ? clientSettingsSection.Settings : null;
|
|
}
|
|
}
|
|
}
|