Files
KJD_WebService/TEMUKJD/Program.cs
T

129 lines
4.9 KiB
C#

using System;
using System.IO;
using System.Net;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using TemuKJD.UserForm;
using Work;
namespace TemuKJD
{
static class Program
{
public static bool IsInit = false;
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
MainForm.CefInit();
Application.EnableVisualStyles();
Application.SetCompatibleTextRenderingDefault(false);
//安装客户端必备字体
InstallFont(Application.StartupPath + @"\Fonts\STZHONGS.TTF");
System.Threading.Tasks.Task.Run(() =>
{
new Initialization().Get();
StartUp.Init();
IsInit = true;
});
Application.Run(new InitForm());
}
[DllImport("kernel32.dll", SetLastError = true)]
public static extern int WriteProfileString(string lpszSection, string lpszKeyName, string lpszString);
[DllImport("gdi32")]
public static extern int AddFontResource(string lpFileName);
/// <summary>
/// 安装字体
/// </summary>
/// <param name="fontFilePath">字体文件全路径</param>
/// <returns>是否成功安装字体</returns>
/// <exception cref="UnauthorizedAccessException">不是管理员运行程序</exception>
/// <exception cref="Exception">字体安装失败</exception>
public static bool InstallFont(string fontFilePath)
{
try
{
System.Security.Principal.WindowsIdentity identity = System.Security.Principal.WindowsIdentity.GetCurrent();
System.Security.Principal.WindowsPrincipal principal = new System.Security.Principal.WindowsPrincipal(identity);
//判断当前登录用户是否为管理员
if (principal.IsInRole(System.Security.Principal.WindowsBuiltInRole.Administrator) == false)
{
throw new UnauthorizedAccessException("当前用户无管理员权限,无法安装字体。");
}
//获取Windows字体文件夹路径
string fontPath = Path.Combine(System.Environment.GetEnvironmentVariable("WINDIR"), "fonts", Path.GetFileName(fontFilePath));
//检测系统是否已安装该字体
if (!File.Exists(fontPath))
{
// File.Copy(System.Windows.Forms.Application.StartupPath + "\\font\\" + FontFileName, FontPath); //font是程序目录下放字体的文件夹
//将某路径下的字体拷贝到系统字体文件夹下
File.Copy(fontFilePath, fontPath); //font是程序目录下放字体的文件夹
AddFontResource(fontPath);
//Res = SendMessage(HWND_BROADCAST, WM_FONTCHANGE, 0, 0);
//WIN7下编译会出错,不清楚什么问题。注释就行了。
//安装字体
WriteProfileString("fonts", Path.GetFileNameWithoutExtension(fontFilePath) + "(TrueType)", Path.GetFileName(fontFilePath));
}
}
catch (Exception ex)
{
//throw new Exception(string.Format($"[{Path.GetFileNameWithoutExtension(fontFilePath)}] 字体安装失败!原因:{ex.Message}"));
}
return true;
}
static string Md5(string file)
{
if (!File.Exists(file))
return "";
try
{
FileStream get_file = new FileStream(file, FileMode.Open, FileAccess.Read, FileShare.Read);
System.Security.Cryptography.MD5CryptoServiceProvider get_md5 = new System.Security.Cryptography.MD5CryptoServiceProvider();
byte[] hash_byte = get_md5.ComputeHash(get_file);
string resule = System.BitConverter.ToString(hash_byte);
resule = resule.Replace("-", "");
get_file.Close();
return resule;
}
catch (Exception e)
{
return e.ToString();
}
}
/// <summary>
/// 下载文件
/// </summary>
/// <param name="Urlstring">下载地址</param>
/// <param name="FilePath">保存目录</param>
/// <returns></returns>
public static bool DownloadFile(string Urlstring, string FilePath)
{
try
{
WebClient mywebclient = new WebClient();
if (File.Exists(FilePath))
File.Delete(FilePath);
mywebclient.DownloadFile(Urlstring, FilePath);
return true;
}
catch
{
return false;
}
}
}
}