master
sunhonglei 5 months ago
parent f2d579bd2f
commit c5a85c881d
  1. 8
      UserManagerTool/InstallWindow.xaml
  2. 179
      UserManagerTool/InstallWindow.xaml.cs
  3. 9
      UserManagerTool/UserManagerTool.csproj

@ -195,7 +195,7 @@
<!-- <Button Grid.Column="2" Content="浏览" Width="40" x:Name="BrowseWorkSpace" Click="ButtonBase_OnClick"/> -->
<!-- </Grid> -->
<Grid Grid.Row="9" Grid.Column="0" Margin="20 10 20 20" Height="60">
<Grid Grid.Row="9" Grid.Column="0" Margin="20 10 20 20" Height="110">
<Grid.ColumnDefinitions>
<ColumnDefinition Width="Auto" />
<ColumnDefinition Width="*" />
@ -204,9 +204,15 @@
<Grid.RowDefinitions>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
<RowDefinition Height="*"/>
</Grid.RowDefinitions>
<Button Grid.Row="0" Grid.Column="1" Style="{StaticResource InstallButton}" Content="开始部署" x:Name="Install" Click="ButtonBase_OnClick"/>
<CheckBox Grid.Row="1" Grid.Column="1" Content="部署成功后,在桌面创建快捷方式。" Height="20" IsChecked="True" Margin="0 5 0 0" x:Name="CheckBox"/>
<!-- 进度条 -->
<ProgressBar Grid.Row="2" Grid.Column="1" x:Name="InstallProgressBar" Height="20" Margin="0,10,0,0" Visibility="Hidden"/>
<!-- 当前文件信息 -->
<TextBlock Grid.Row="3" Grid.Column="1" x:Name="StatusTextBlock" Height="20" Text="等待开始..." Visibility="Hidden"/>
</Grid>
</Grid>
</Window>

@ -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();

@ -20,6 +20,15 @@
<None Remove="instal.ico" />
<Resource Include="instal.ico" />
<None Remove="Images\install.png" />
<COMReference Include="IWshRuntimeLibrary">
<WrapperTool>tlbimp</WrapperTool>
<VersionMinor>0</VersionMinor>
<VersionMajor>1</VersionMajor>
<Guid>f935dc20-1cf0-11d0-adb9-00c04fd58a0b</Guid>
<Lcid>0</Lcid>
<Isolated>false</Isolated>
<EmbedInteropTypes>true</EmbedInteropTypes>
</COMReference>
<Resource Include="Images\install.png" />
</ItemGroup>

Loading…
Cancel
Save