Files
2026-05-29 10:07:05 +08:00

348 lines
14 KiB
C#
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MisDataName
{
public delegate void NormalDel();
public partial class AssemblyManagement : Form
{
public static Hashtable ExternalAssembly = new Hashtable();
public static Hashtable MisAssembly_UnLoad = new Hashtable();//储存所有已卸载的程序集
public static NormalDel UploadAssemblyDel ;
public static string UpLoadObject = "";
public AssemblyManagement()
{
CreateAssemblyFolder();
InitializeComponent();
UploadAssemblyDel = new NormalDel(UploadAssembly);
InitAssemblyList();
}
/// <summary>
/// 创建程序集文件夹
/// </summary>
void CreateAssemblyFolder()
{
try
{
//获取程序集路径
string AssemblyCreatePath = System.IO.Directory.GetCurrentDirectory();
//判断当前路径是否存在对应文件夹
if (!Directory.Exists(AssemblyCreatePath + "\\Assembly_External"))
{
Directory.CreateDirectory(AssemblyCreatePath + "\\Assembly_External");
}
if (!Directory.Exists(AssemblyCreatePath + "\\Assembly_Recycle"))
{
Directory.CreateDirectory(AssemblyCreatePath + "\\Assembly_Recycle");
}
}
catch (Exception err)
{
SystemFramework.ApplicationLog.WriteLog(err, err.Message);
}
}
/// <summary>
/// 初始化回收文件夹,删除内部所有文件
/// </summary>
public static void InitAssembly_Recycle()
{
try
{
DirectoryInfo folder_Recycle = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory() + "\\Assembly_Recycle");
foreach (FileInfo file in folder_Recycle.GetFiles("*.dll"))
{
file.Delete();
}
}
catch (Exception err)
{
SystemFramework.ApplicationLog.WriteLog(err, err.Message);
}
}
/// <summary>
/// 初始化程序集列表
/// </summary>
void InitAssemblyList()
{
try
{
ExternalAssembly = new Hashtable();
listView_Assembly.Items.Clear();
ListViewItem AssemblyItem;
string[] AssemblyStrArrary = new string[3];
int i = 1, k = 0;
string[] AssemblyKeys = new string[MesDataFunction.OpcOperation.Hashtable_assembly.Count];
foreach (string _key in MesDataFunction.OpcOperation.Hashtable_assembly.Keys)
{
AssemblyKeys[k] = _key;
k++;
}
Array.Sort(AssemblyKeys);
for (int j = 0; j < AssemblyKeys.Length; j++)
{
AssemblyStrArrary[0] = i.ToString();
AssemblyStrArrary[1] = AssemblyKeys[j];
if (MesDataFunction.OpcOperation.Hashtable_assembly[AssemblyKeys[j]] != null)
{
AssemblyStrArrary[2] = ((System.Reflection.Assembly)MesDataFunction.OpcOperation.Hashtable_assembly[AssemblyKeys[j]]).FullName.Split(',')[0] + ".dll";
}
else
{
AssemblyStrArrary[2] = "";
}
AssemblyItem = new ListViewItem(AssemblyStrArrary);
listView_Assembly.Items.Add(AssemblyItem);
i++;
}
//初始化外部程序集
DirectoryInfo folder_External = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory() + "\\Assembly_External");
foreach (FileInfo file in folder_External.GetFiles("*.dll"))
{
AssemblyStrArrary[0] = i.ToString();
AssemblyStrArrary[1] = "未挂载";
AssemblyStrArrary[2] = "..MisData" + file.FullName.Replace("MisData\\Assembly_External", "!\\Assembly_External").Split('!')[1];
AssemblyItem = new ListViewItem(AssemblyStrArrary);
listView_Assembly.Items.Add(AssemblyItem);
i++;
}
//初始化外部程序集
DirectoryInfo folder_Recycle = new DirectoryInfo(System.IO.Directory.GetCurrentDirectory() + "\\Assembly_Recycle");
foreach (FileInfo file in folder_Recycle.GetFiles("*.dll"))
{
AssemblyStrArrary[0] = i.ToString();
AssemblyStrArrary[1] = "已卸载";
AssemblyStrArrary[2] = "..MisData" + file.FullName.Replace("MisData\\Assembly_Recycle", "!\\Assembly_Recycle").Split('!')[1];
AssemblyItem = new ListViewItem(AssemblyStrArrary);
listView_Assembly.Items.Add(AssemblyItem);
i++;
}
}
catch (Exception err)
{
SystemFramework.ApplicationLog.WriteLog(err, err.Message);
}
}
private void button_Exit_Click(object sender, EventArgs e)
{
Close();
}
private void button_AddAssembly_Click(object sender, EventArgs e)
{
openFileDialog_OpenAssembly.ShowDialog();
//刷新listview
}
private void button_UpLoad_Click(object sender, EventArgs e)
{
try
{
if (listView_Assembly.SelectedItems.Count > 0)
{
if (listView_Assembly.SelectedItems[0].SubItems[2].Text.Length > 3)
{
if (listView_Assembly.SelectedItems[0].SubItems[2].Text.Split('\\').Length <= 1)
{
MessageBox.Show("当前程序集已装载,禁止重复装载!");
return;
}
UploadAssembly UploadAssemblyForm = new MisDataName.UploadAssembly(listView_Assembly.SelectedItems[0].SubItems[2].Text);
UploadAssemblyForm.ShowDialog();
InitAssemblyList();
}
else
{
MessageBox.Show("请选择要操作的程序集。");
}
}
else
{
MessageBox.Show("请选择要操作的程序集。");
}
}
catch (Exception err)
{
SystemFramework.ApplicationLog.WriteLog(err, err.Message);
}
}
private void button_DownLoad_Click(object sender, EventArgs e)
{
try
{
if (listView_Assembly.SelectedItems.Count > 0)
{
string AssemblyFileName = listView_Assembly.SelectedItems[0].SubItems[1].Text;
if (AssemblyFileName.Substring(0, 3) != "Fun")
{
MessageBox.Show("当前程序集未被引用,请选择要卸载的服务!");
return;
}
List<string> DownLoadServerName = new List<string>();
//卸载所有调用当前程序集服务的程序集
System.Reflection.Assembly DownLoadAssembly = ((System.Reflection.Assembly)MesDataFunction.OpcOperation.Hashtable_assembly[AssemblyFileName]);
//遍历所有对当前程序集有调用的服务
foreach (string _key in MesDataFunction.OpcOperation.Hashtable_assembly.Keys)
{
if (MesDataFunction.OpcOperation.Hashtable_assembly[_key] == null)
continue;
if (((System.Reflection.Assembly)MesDataFunction.OpcOperation.Hashtable_assembly[_key]).FullName == DownLoadAssembly.FullName)
{
DownLoadServerName.Add(_key);
}
}
foreach (string _key in DownLoadServerName)
{
MesDataFunction.OpcOperation.Hashtable_assembly[_key] = null;
}
//将卸载的DLL文件移出到回收文件
File.Move(System.IO.Directory.GetCurrentDirectory() + "\\" + listView_Assembly.SelectedItems[0].SubItems[2].Text, System.IO.Directory.GetCurrentDirectory() + "\\Assembly_Recycle\\" + listView_Assembly.SelectedItems[0].SubItems[2].Text.Replace(".dll", "") + "_" + DateTime.Now.ToUniversalTime().ToString().Replace('/', '_').Replace(' ', '_').Replace(':', '_') + ".dll");
InitAssemblyList();
MessageBox.Show("程序集卸载成功!");
}
else
{
MessageBox.Show("请选择要卸载的程序集。");
}
}
catch (Exception err)
{
SystemFramework.ApplicationLog.WriteLog(err, err.Message);
}
}
/// <summary>
/// 装载程序集
/// </summary>
void UploadAssembly()
{
try
{
//获取选中行的 dll 名称
string BinPath = System.IO.Directory.GetCurrentDirectory();
string AssemblyFileName = listView_Assembly.SelectedItems[0].SubItems[2].Text.Replace("..MisData\\", "");
AssemblyFileName = AssemblyFileName.Split('\\')[1].ToString();//程序集文件名称
//此步骤需要将所有引用的同名DLL全部替换
System.Reflection.Assembly new_LoadAssembly = System.Reflection.Assembly.LoadFile(BinPath + listView_Assembly.SelectedItems[0].SubItems[2].Text.Replace("..MisData", ""));
System.Reflection.Assembly old_LoadAssembly = (System.Reflection.Assembly)MesDataFunction.OpcOperation.Hashtable_assembly[UpLoadObject];
int Compare_i = 0;
foreach (string _key in MesDataFunction.OpcOperation.Hashtable_assembly.Keys)
{
if (((System.Reflection.Assembly)MesDataFunction.OpcOperation.Hashtable_assembly[_key]) == null) continue;
if (((System.Reflection.Assembly)MesDataFunction.OpcOperation.Hashtable_assembly[_key]).FullName == new_LoadAssembly.FullName && _key != UpLoadObject)
{
//提示存在同名程序集 若要替换 请先卸载同名程序集
Compare_i++;
}
}
if (Compare_i > 0)
{
//存在多个引用项
MessageBox.Show("系统存在与当前程序集同名的程序集引用,如果要引用新程序集,请提前卸载其他服务引用的同名程序集:", "提示:");
return;
}
else
{
//移出旧程序集
if (old_LoadAssembly != null)
{
File.Move(System.IO.Directory.GetCurrentDirectory() + "\\" + old_LoadAssembly.FullName.Split(',')[0] + ".dll", System.IO.Directory.GetCurrentDirectory() + "\\Assembly_Recycle\\" + old_LoadAssembly.FullName.Split(',')[0] + "_" + DateTime.Now.ToUniversalTime().ToString().Replace('/', '_').Replace(' ', '_').Replace(':', '_') + ".dll");
}
MesDataFunction.OpcOperation.Hashtable_assembly[UpLoadObject] = new_LoadAssembly;
//移动原程序集 然后移动新程序集 系统文件夹是否存在同名Dll文件
if (File.Exists(System.IO.Directory.GetCurrentDirectory() + "\\" + AssemblyFileName))
{
//将同名DLL文件移出到回收文件
File.Move(System.IO.Directory.GetCurrentDirectory() + "\\" + AssemblyFileName, System.IO.Directory.GetCurrentDirectory() + "\\Assembly_Recycle\\" + AssemblyFileName.Replace(".dll", "") + "_" + DateTime.Now.ToUniversalTime().ToString().Replace('/', '_').Replace(' ', '_').Replace(':', '_') + ".dll");
}
File.Move(System.IO.Directory.GetCurrentDirectory() + "\\" + listView_Assembly.SelectedItems[0].SubItems[2].Text.Replace("..MisData\\", ""), System.IO.Directory.GetCurrentDirectory() + "\\" + (AssemblyFileName.Split('_')[0]).Replace(".dll", "") + ".dll");//移入新程序集
MessageBox.Show("程序集装载成功!");
}
}
catch (Exception err)
{
SystemFramework.ApplicationLog.WriteLog(err, err.Message);
}
}
private void openFileDialog_OpenAssembly_FileOk(object sender, CancelEventArgs e)
{
try
{
//将文件复制到Assembly_External 文件夹
if (openFileDialog_OpenAssembly.SafeFileName.Split('.')[openFileDialog_OpenAssembly.SafeFileName.Split('.').Length - 1] != "dll")
{
MessageBox.Show("添加的文件无效请选择需要添加的“dll”文件");
return;
}
FileInfo file = new FileInfo(openFileDialog_OpenAssembly.FileName);
file.CopyTo(System.IO.Directory.GetCurrentDirectory() + "\\Assembly_External\\" + openFileDialog_OpenAssembly.SafeFileName, true);
InitAssemblyList();
}
catch (Exception err)
{
SystemFramework.ApplicationLog.WriteLog(err, err.Message);
}
}
private void button_Refresh_Click(object sender, EventArgs e)
{
InitAssemblyList();
}
}
}