|
|
|
@ -1,9 +1,14 @@ |
|
|
|
|
using System.Security.Principal; |
|
|
|
|
using System; |
|
|
|
|
using System.IO; |
|
|
|
|
using System.Security.Principal; |
|
|
|
|
using System.Text.RegularExpressions; |
|
|
|
|
using System.Windows; |
|
|
|
|
using System.Windows.Controls; |
|
|
|
|
using System.Windows.Threading; |
|
|
|
|
using Microsoft.Win32; |
|
|
|
|
using UserManagerTool.Model.Helper; |
|
|
|
|
using Path = System.IO.Path; |
|
|
|
|
using IWshRuntimeLibrary; |
|
|
|
|
|
|
|
|
|
namespace UserManagerTool; |
|
|
|
|
|
|
|
|
@ -13,8 +18,9 @@ public partial class InstallWindow : Window |
|
|
|
|
{ |
|
|
|
|
InitializeComponent(); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void InstallSprak() |
|
|
|
|
static decimal FileIndex = 0; |
|
|
|
|
static decimal TotalFileCount = 0; |
|
|
|
|
private async void InstallSprak() |
|
|
|
|
{ |
|
|
|
|
/*** |
|
|
|
|
* 1.校验输入内容是否全部输入 |
|
|
|
@ -27,13 +33,143 @@ public partial class InstallWindow : Window |
|
|
|
|
*/ |
|
|
|
|
if (InstallSprakCheck()) |
|
|
|
|
{ |
|
|
|
|
//检查资源完整性 |
|
|
|
|
Install.IsEnabled = false; |
|
|
|
|
TestCutUrl.IsEnabled = false; |
|
|
|
|
DeviceCode.IsEnabled = false; |
|
|
|
|
DefaultPassword.IsEnabled = false; |
|
|
|
|
DefaultUserName.IsEnabled = false; |
|
|
|
|
BrowseInstallPath.IsEnabled = false; |
|
|
|
|
InstallPath.IsEnabled = false; |
|
|
|
|
CutUrl.IsEnabled = false; |
|
|
|
|
|
|
|
|
|
InstallProgressBar.Visibility = Visibility.Visible; |
|
|
|
|
StatusTextBlock.Visibility = Visibility.Visible; |
|
|
|
|
string strBaseUrl = AppDomain.CurrentDomain.BaseDirectory; |
|
|
|
|
|
|
|
|
|
FileIndex = 0; |
|
|
|
|
bool copyResult = await CopyFile(strBaseUrl); |
|
|
|
|
if (!copyResult) |
|
|
|
|
{ |
|
|
|
|
Install.IsEnabled = true; |
|
|
|
|
TestCutUrl.IsEnabled = true; |
|
|
|
|
DeviceCode.IsEnabled = true; |
|
|
|
|
DefaultPassword.IsEnabled = true; |
|
|
|
|
DefaultUserName.IsEnabled = true; |
|
|
|
|
BrowseInstallPath.IsEnabled = true; |
|
|
|
|
InstallPath.IsEnabled = true; |
|
|
|
|
CutUrl.IsEnabled = true; |
|
|
|
|
return; |
|
|
|
|
} |
|
|
|
|
var dbPath = Path.Combine(InstallPath.Text, "SparkDB.db"); |
|
|
|
|
InsertUserData(dbPath); |
|
|
|
|
if (CheckBox.IsChecked??false) |
|
|
|
|
{ |
|
|
|
|
string filePath = Path.Combine(InstallPath.Text, "SparkClient.exe"); ; // 要创建快捷方式的文件路径 |
|
|
|
|
string shortcutName = "星辉"; // 快捷方式名称 |
|
|
|
|
string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); // 桌面路径 |
|
|
|
|
CreateShortcut(filePath, desktopPath, shortcutName); |
|
|
|
|
} |
|
|
|
|
StatusTextBlock.Text = "安装完成"; |
|
|
|
|
MessageBox.Show("部署完成"); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private async Task<bool> CopyFile(string strBaseUrl) |
|
|
|
|
{ |
|
|
|
|
string binPath = Path.Combine(strBaseUrl, "bin"); |
|
|
|
|
string dbPath = Path.Combine(strBaseUrl, "config"); |
|
|
|
|
decimal FileCount = CountFile(binPath); |
|
|
|
|
FileCount += CountFile(dbPath); |
|
|
|
|
TotalFileCount = FileCount; |
|
|
|
|
if (Directory.GetFiles(InstallPath.Text).Length > 0) |
|
|
|
|
{ |
|
|
|
|
MessageBox.Show("请选择空白目录!"); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
bool copyResult = await CopyDirectory(binPath, InstallPath.Text); |
|
|
|
|
if (!copyResult) { |
|
|
|
|
return copyResult; |
|
|
|
|
} |
|
|
|
|
copyResult = await CopyDirectory(dbPath, InstallPath.Text); |
|
|
|
|
return copyResult; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private decimal CountFile(string path) |
|
|
|
|
{ |
|
|
|
|
if (Directory.Exists(path)) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
int fileCount = Directory.GetFiles(path, "*.*", SearchOption.AllDirectories).Length; |
|
|
|
|
return fileCount; |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
return 0; |
|
|
|
|
} |
|
|
|
|
async Task<bool> CopyDirectory(string sourceDir, string destinationDir) |
|
|
|
|
{ |
|
|
|
|
// 获取源目录的信息 |
|
|
|
|
DirectoryInfo dir = new DirectoryInfo(sourceDir); |
|
|
|
|
|
|
|
|
|
// 如果目标目录不存在,则创建它 |
|
|
|
|
if (!Directory.Exists(destinationDir)) |
|
|
|
|
{ |
|
|
|
|
Directory.CreateDirectory(destinationDir); |
|
|
|
|
} |
|
|
|
|
else |
|
|
|
|
{ |
|
|
|
|
|
|
|
|
|
} |
|
|
|
|
if (!Directory.Exists(sourceDir)) |
|
|
|
|
{ |
|
|
|
|
MessageBox.Show("资源不完整无法安装"); |
|
|
|
|
return false; |
|
|
|
|
} |
|
|
|
|
// 复制所有文件 |
|
|
|
|
foreach (FileInfo file in dir.GetFiles()) |
|
|
|
|
{ |
|
|
|
|
string tempPath = Path.Combine(destinationDir, file.Name); |
|
|
|
|
await Task.Run(() => file.CopyTo(tempPath, false)); |
|
|
|
|
FileIndex++; |
|
|
|
|
int progress = (int)(FileIndex / TotalFileCount * 100); |
|
|
|
|
Dispatcher.Invoke(() => |
|
|
|
|
{ |
|
|
|
|
InstallProgressBar.Value = progress; |
|
|
|
|
StatusTextBlock.Text = $"正在复制: {file.Name} ({FileIndex}/{TotalFileCount})"; |
|
|
|
|
}); |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
// 递归复制所有子目录 |
|
|
|
|
foreach (DirectoryInfo subDir in dir.GetDirectories()) |
|
|
|
|
{ |
|
|
|
|
string tempPath = Path.Combine(destinationDir, subDir.Name); |
|
|
|
|
await CopyDirectory(subDir.FullName, tempPath); |
|
|
|
|
} |
|
|
|
|
return !false; |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
private void InsertUserData(string dbPath) |
|
|
|
|
{ |
|
|
|
|
DataBaseHelper.CreateConnection(dbPath); |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
string roleId = "01"; |
|
|
|
|
string insertSql = $"insert into user (USER_NAME,USER_PASSWORD,ROLE_ID,GUID) " + |
|
|
|
|
$" values ('{DefaultUserName.Text}','{Common.GenerateMd5Hash(DefaultPassword.Text)}','{roleId}','{Guid.NewGuid()}');"; |
|
|
|
|
int result = DataBaseHelper.ExecuteNonQuery(insertSql); |
|
|
|
|
} |
|
|
|
|
finally |
|
|
|
|
{ |
|
|
|
|
DataBaseHelper.CloseConnection(); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
private bool InstallSprakCheck() |
|
|
|
|
{ |
|
|
|
|
/*** |
|
|
|
@ -150,6 +286,39 @@ public partial class InstallWindow : Window |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
static void CreateShortcut(string targetFilePath, string shortcutPath, string shortcutName) |
|
|
|
|
{ |
|
|
|
|
try |
|
|
|
|
{ |
|
|
|
|
// 创建WshShell对象 |
|
|
|
|
WshShell shell = new WshShell(); |
|
|
|
|
|
|
|
|
|
// 快捷方式的完整路径 |
|
|
|
|
string shortcutLocation = Path.Combine(shortcutPath, shortcutName + ".lnk"); |
|
|
|
|
|
|
|
|
|
// 创建快捷方式对象 |
|
|
|
|
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(shortcutLocation); |
|
|
|
|
|
|
|
|
|
// 设置快捷方式的目标路径 |
|
|
|
|
shortcut.TargetPath = targetFilePath; |
|
|
|
|
|
|
|
|
|
// 设置快捷方式的工作目录(可选) |
|
|
|
|
shortcut.WorkingDirectory = Path.GetDirectoryName(targetFilePath); |
|
|
|
|
|
|
|
|
|
//// 设置快捷方式的描述(可选) |
|
|
|
|
//shortcut.Description = "快捷方式描述"; |
|
|
|
|
|
|
|
|
|
//// 设置快捷方式的图标(可选) |
|
|
|
|
//shortcut.IconLocation = "shell32.dll,1"; // 使用系统图标,或者指定图标文件路径 |
|
|
|
|
|
|
|
|
|
// 保存快捷方式 |
|
|
|
|
shortcut.Save(); |
|
|
|
|
} |
|
|
|
|
catch (Exception ex) |
|
|
|
|
{ |
|
|
|
|
Console.WriteLine("创建快捷方式时出错: " + ex.Message); |
|
|
|
|
} |
|
|
|
|
} |
|
|
|
|
private void InstallWindow_OnLoaded(object sender, RoutedEventArgs e) |
|
|
|
|
{ |
|
|
|
|
WindowsIdentity identity = WindowsIdentity.GetCurrent(); |
|
|
|
|