图1
图1中IWshRuntimeLibray是在“引用”-->“添加引用”,选择“COM”标签页中,选择“Windows Script Host Object Model”
图2
图3
图4
图5
图6
图7
Install项目-->JLInstaller文件代码:
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration.Install;
using Library.Application;
using System.Windows.Forms;
using System.IO;
namespace Install
{
[RunInstaller(true)]
public partial class JLInstaller : Installer
{
public JLInstaller()
{
InitializeComponent();
}
/// <summary>
/// 安装
/// </summary>
/// <param name="stateSaver"></param>
public override void Install(System.Collections.IDictionary stateSaver)
{
base.Install(stateSaver);
InstallApplication();
}
/// <summary>
/// 安装后
/// </summary>
/// <param name="savedState"></param>
protected override void OnAfterInstall(System.Collections.IDictionary savedState)
{
base.OnAfterInstall(savedState);
}
/// <summary>
/// 卸载
/// </summary>
/// <param name="savedState"></param>
public override void Uninstall(System.Collections.IDictionary savedState)
{
string _TargetDir = this.Context.Parameters["TargetDir"];
Logging.WriterInstallLog(_TargetDir, "开始执行卸载!");
try
{
//删除快捷
CreatingShortcuts.DeleteDesktopShortcuts();
CreatingShortcuts.DeleteProgramsShortcuts();
}
catch
{
Logging.WriterInstallLog(_TargetDir, "执行卸载失败!");
}
base.Uninstall(savedState);
}
/// <summary>
/// 卸载后
/// </summary>
/// <param name="savedState"></param>
protected override void OnAfterUninstall(System.Collections.IDictionary savedState)
{
string _TargetDir = this.Context.Parameters["TargetDir"];
try
{
//删除安装目录
CreatingShortcuts.DeleteStallerDir(_TargetDir);
}
catch
{
Logging.WriterInstallLog(_TargetDir, "执行卸载成功!");
}
base.OnAfterUninstall(savedState);
}
/// <summary>
/// 回滚
/// </summary>
/// <param name="savedState"></param>
public override void Rollback(IDictionary savedState)
{
//删除快捷
CreatingShortcuts.DeleteDesktopShortcuts();
CreatingShortcuts.DeleteProgramsShortcuts();
//string filePath = base.Context.Parameters["TargetDir"];
//Directory.Delete(filePath, true);
base.Rollback(savedState);
}
/// <summary>
/// 安装流程
/// </summary>
private void InstallApplication()
{
string filePath = base.Context.Parameters["TargetDir"];
Logging.WriterInstallLog(filePath, "--------------------------");
Logging.WriterInstallLog(filePath, "开始安装现金流通管理系统");
int num = 0;
try
{
//第三方控件注册
if (RegisterInstallInfo.RegisterAdd2Assembly(filePath))
{
Logging.WriterInstallLog(filePath, "注册成功!");
}
else
{
Logging.WriterInstallLog(filePath, "注册失败!");
num++;
}
//创建快捷方式
if (CreatingShortcuts.CreatingProgramMenuFolder(filePath) && CreatingShortcuts.CreatingShortcut(filePath))
{
Logging.WriterInstallLog(filePath, "成功创建快捷方式!");
}
else
{
Logging.WriterInstallLog(filePath, "创建快捷方式失败!");
num++;
}
if (num != 0)
{
throw new Exception("安装失败!");
}
else
{
Logging.WriterInstallLog(filePath, "安装成功!");
Logging.WriterInstallLog(filePath, "-----------------------");
}
}
catch (Exception exception)
{
Logging.WriterInstallLog(filePath, "安装失败!");
Logging.WriterInstallLog(filePath, "-----------------------");
Logging.WriterInstallLog(filePath, exception.ToString());
Logging.WriterInstallLog(filePath, "------------------------");
MessageBox.Show("安装失败,具体安装内容请查看安装日志InstallLogyyyymmdd.txt!");
throw new Exception("安装失败,安装程序已经停止!");
}
}
}
}
Library项目-->Application目录-->CreatingShortcuts文件代码:
using System;
using System.Collections.Generic;
using System.Text;
using IWshRuntimeLibrary;
namespace Library.Application
{
public class CreatingShortcuts
{
/// <summary>
/// 创建桌面快捷方式
/// </summary>
/// <param name="FilePath">程序安装路径</param>
public static bool CreatingShortcut(string FilePath)
{
bool Result = false;
try
{
WshShell shell = new WshShell();
string FolderPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "//" + "系统.lnk";
if (System.IO.File.Exists(FolderPath)) // 如果快捷方式已经存在,则删除
{
System.IO.File.Delete(FolderPath);
}
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(FolderPath);
shortcut.TargetPath = FilePath + "WindowsFormsApplication2.exe";
shortcut.WorkingDirectory = FilePath;
shortcut.WindowStyle = 1;
shortcut.Description = "系统";
shortcut.IconLocation = FilePath + "JuLong.ico";
shortcut.Save();
Result = true;
}
catch
{
}
return Result;
}
/// <summary>
/// 创建开始菜单快捷方式
/// </summary>
/// <param name="FilePath">程序安装路径</param>
/// <returns></returns>
public static bool CreatingProgramMenuFolder(string FilePath)
{
bool Result = false;
try
{
if (CreateFile())
{
WshShell shell = new WshShell();
string FolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//系统" + "//" + "系统.lnk";
if (System.IO.File.Exists(FolderPath)) // 如果快捷方式已经存在,则删除
{
System.IO.File.Delete(FolderPath);
}
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(FolderPath);
shortcut.TargetPath = FilePath + "WindowsFormsApplication2.exe";
shortcut.WorkingDirectory = FilePath;
shortcut.WindowStyle = 1;
shortcut.Description = "系统";
shortcut.IconLocation = FilePath + "JuLong.ico";
shortcut.Save();
UninstallApplication(FilePath);
Result = true;
}
}
catch
{
}
return Result;
}
/// <summary>
/// 在开始程序菜单添加卸载
/// </summary>
/// <param name="FilePath">安装路径</param>
public static void UninstallApplication(string FilePath)
{
try
{
WshShell shell = new WshShell();
string FolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//系统" + "//" + "卸载.lnk";
if (System.IO.File.Exists(FolderPath))
{
System.IO.File.Delete(FolderPath);
}
IWshShortcut shortcut = (IWshShortcut)shell.CreateShortcut(FolderPath);
shortcut.TargetPath = FilePath + "msiexec.exe";
shortcut.WorkingDirectory = FilePath;
shortcut.WindowStyle = 1;
shortcut.Description = "系统";
shortcut.IconLocation = FilePath + "Uninstall.ico";
shortcut.Arguments = "/x{FF97BF66-D630-4B27-BD58-083F3BBB4E67}";
shortcut.Save();
}
catch
{
}
}
/// <summary>
/// 删除开始菜单快捷方式
/// </summary>
public static void DeleteProgramsShortcuts()
{
string FolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//系统";
if (System.IO.Directory.Exists(FolderPath)) // 如果快捷方式已经存在,则删除
{
System.IO.Directory.Delete(FolderPath, true);
}
}
/// <summary>
/// 删除桌面快捷方式
/// </summary>
public static void DeleteDesktopShortcuts()
{
string FolderPath = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory) + "//" + "系统.lnk";
if (System.IO.File.Exists(FolderPath)) // 如果快捷方式已经存在,则删除
{
System.IO.File.Delete(FolderPath);
}
}
/// <summary>
/// 删除安装目录
/// </summary>
/// <param name="StallPath"></param>
public static void DeleteStallerDir(string StallPath)
{
if (StallPath != "")
{
if (System.IO.Directory.Exists(StallPath))
{
System.IO.Directory.Delete(StallPath, true);
}
}
}
/// <summary>
/// 在开始程序里面创建文件夹
/// </summary>
/// <returns></returns>
private static bool CreateFile()
{
bool Result = false;
string FolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Programs) + "//系统";
if (!System.IO.Directory.Exists(FolderPath))
{
System.IO.Directory.CreateDirectory(FolderPath);
Result = true;
}
else
{
System.IO.Directory.Delete(FolderPath, true);
System.IO.Directory.CreateDirectory(FolderPath);
Result = true;
}
return Result;
}
}
}
Library项目-->Application目录-->Logging文件代码:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Logging;
namespace Library.Application
{
public class Logging
{
private static string m_InstallLog = "InstallLog.txt";
public static void WriterInstallLog(string FilePath, string Message)
{
string _tmpFilePath = FilePath + m_InstallLog;
WriterTextLog.WriterLog(_tmpFilePath, Message);
}
}
}
Library项目-->Application目录-->RegisterInstallInfo文件代码:
using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Win32;
using System.Diagnostics;
using System.Net;
using System.IO;
using System.Windows.Forms;
namespace Library.Application
{
public class RegisterInstallInfo
{
/// <summary>
/// 破解
/// </summary>
/// <param name="FilePath">路径</param>
/// <returns></returns>
public static bool RegisterAdd2Assembly(string FilePath)
{
bool _Result = false;
try
{
if (WriteRunbat(FilePath))
{
Process MyProcess = new Process();
string filename = string.Format("{0}Register.bat", FilePath + "Register//");
MyProcess.StartInfo.FileName = string.Format("{0}Register.bat", FilePath + "Register//");
//MessageBox.Show(filename);
MyProcess.StartInfo.Arguments = "";
MyProcess.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
MyProcess.Start();
MyProcess.WaitForExit();
MyProcess.Close();
_Result = true;
if (_Result)
{
Directory.Delete(FilePath + "//Register", true);
}
}
}
catch
{
return false;
}
return _Result;
}
private static bool WriteRunbat(string FilePath)
{
bool _Result = false;
if (FilePath != "")
{
using (StreamWriter Mystream = File.CreateText(string.Format("{0}Register.bat", FilePath + "//Register//")))
{
char[] ch = new char[] { '//' };
string FilePathName = FilePath + "Register";
string[] st_splitpath = FilePathName.Split(ch);
Mystream.WriteLine(string.Format("{0} {1}", "cd", "//"));
Mystream.WriteLine(string.Format("{0}", st_splitpath[0]));
for (int i = 1; i < st_splitpath.Length; i++)
{
Mystream.WriteLine(string.Format("{0} {1}", "cd", st_splitpath[i]));
}
Mystream.WriteLine(string.Format("{0}", "echo off"));
Mystream.WriteLine(string.Format("{0}", "color 0a"));
Mystream.WriteLine(string.Format("{0}", "gacutil -u xxxx"));
Mystream.WriteLine(string.Format("{0}", @"mkdir %windir%/assembly/GAC_MSIL/xxxxxxx"));
Mystream.WriteLine(string.Format("{0}", @"copy xxxx.dll %windir%/assembly/GAC_MSIL/xxxxx"));
Mystream.WriteLine(string.Format("{0}", "gacutil -u DevExpress.Utils.v9.3"));
Mystream.WriteLine(string.Format("{0}", @"mkdir %windir%/assembly/GAC_MSIL/xxxxxx"));
Mystream.WriteLine(string.Format("{0}", @"copy xxxx.dll %windir%/assembly/GAC_MSIL/xxxxx"));
Mystream.WriteLine(string.Format("{0}", "echo **************************************"));
Mystream.WriteLine(string.Format("{0}", "echo OK"));
Mystream.WriteLine(string.Format("{0}", "echo on"));
_Result = true;
}
}
return _Result;
}
}
}
Logging项目-->EventLogClass文件
using System;
using System.Collections.Generic;
using System.Text;
using System.Diagnostics;
using System.Runtime.CompilerServices;
namespace Logging
{
public class EventLogClass
{
private EventLogClass()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
#region 在计算机的注册表中搜索给定的事件源
/// <summary>
/// 在本地计算机的注册表中搜索给定的事件源
/// </summary>
/// <param name="sourceName">事件源名称</param>
/// <returns>存在返回true,不存在返回false</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public static bool SourceExists(string sourceName)
{
return EventLog.SourceExists(sourceName);
}
/// <summary>
/// 在指定计算机的注册表中搜索给定的事件源
/// </summary>
/// <param name="sourceName">事件源名称</param>
/// <param name="machineName">计算机名,对于本地计算机则为空字符串</param>
/// <returns>存在返回true,不存在返回false</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public static bool SourceExists(string sourceName, string machineName)
{
return EventLog.SourceExists(sourceName, machineName);
}
#endregion
#region 确定指定的日志是否存在
/// <summary>
/// 确定指定的日志是否存在
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="machineName">计算机名,对于本地计算机则为"."</param>
/// <returns>存在返回true,不存在返回false</returns>
public static bool Exists(string logName, string machineName)
{
return EventLog.Exists(logName, machineName);
}
/// <summary>
/// 确定指定的日志在本地机器上是否存在
/// </summary>
/// <param name="logName">日志名称</param>
/// <returns>存在返回true,不存在返回false</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public static bool Exists(string logName)
{
return EventLogClass.Exists(logName, ".");
}
#endregion
#region 建立一个能够将事件信息写入到系统的特定日志中的应用程序
/// <summary>
/// 建立一个能够将事件信息写入到系统的特定日志中的应用程序
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="machineName">计算机名,对于本地计算机则为"."</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void CreateEventSource(string logName, string sourceName, string machineName)
{
EventLog.CreateEventSource(sourceName, logName, machineName);
}
/// <summary>
/// 在本地建立一个能够将事件信息写入到系统的特定日志中的应用程序
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void CreateEventSource(string logName, string sourceName)
{
EventLogClass.CreateEventSource(sourceName, logName, ".");
}
#endregion
#region 写入事件日志
/// <summary>
/// 写入事件日志
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="message">写入EventLog的信息</param>
/// <param name="typeNum">
/// 0:EventLogEntryType.Error
/// 1:EventLogEntryType.FailureAudit
/// 2:EventLogEntryType.Information
/// 3:EventLogEntryType.SuccessAudit
/// 4:EventLogEntryType.Warning
/// </param>
/// <param name="eventID">事件的应用程序特定标识符</param>
/// <param name="category">与消息关联的应用程序特定子类别</param>
/// <param name="rawData">包含与此项关联的二进制数据的字节数组</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriteEntry(string logName, string sourceName, string message, int typeNum, int eventID, short category, byte[] rawData)
{
EventLogEntryType type = getEventLogEntryType(typeNum);
EventLogClass.WriteEntry(logName, sourceName, message, type, eventID, category, rawData);
}
/// <summary>
/// 写入事件日志
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="message">写入EventLog的信息</param>
/// <param name="typeNum">
/// 0:EventLogEntryType.Error
/// 1:EventLogEntryType.FailureAudit
/// 2:EventLogEntryType.Information
/// 3:EventLogEntryType.SuccessAudit
/// 4:EventLogEntryType.Warning
/// </param>
/// <param name="eventID">事件的应用程序特定标识符</param>
/// <param name="category">与消息关联的应用程序特定子类别</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriteEntry(string logName, string sourceName, string message, int typeNum, int eventID, short category)
{
EventLogEntryType type = getEventLogEntryType(typeNum);
EventLogClass.WriteEntry(logName, sourceName, message, type, eventID, category);
}
/// <summary>
/// 写入事件日志
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="message">写入EventLog的信息</param>
/// <param name="typeNum">
/// 0:EventLogEntryType.Error
/// 1:EventLogEntryType.FailureAudit
/// 2:EventLogEntryType.Information
/// 3:EventLogEntryType.SuccessAudit
/// 4:EventLogEntryType.Warning
/// </param>
/// <param name="eventID">事件的应用程序特定标识符</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriteEntry(string logName, string sourceName, string message, int typeNum, int eventID)
{
EventLogEntryType type = getEventLogEntryType(typeNum);
EventLogClass.WriteEntry(logName, sourceName, message, type, eventID);
}
/// <summary>
/// 写入事件日志
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="message">写入EventLog的信息</param>
/// <param name="typeNum">
/// 0:EventLogEntryType.Error
/// 1:EventLogEntryType.FailureAudit
/// 2:EventLogEntryType.Information
/// 3:EventLogEntryType.SuccessAudit
/// 4:EventLogEntryType.Warning
/// </param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriteEntry(string logName, string sourceName, string message, int typeNum)
{
EventLogEntryType type = getEventLogEntryType(typeNum);
EventLogClass.WriteEntry(logName, sourceName, message, type);
}
#endregion
#region 返回EventLogEntryType类型
/// <summary>
/// 返回EventLogEntryType类型
/// </summary>
/// <param name="typeNum">
/// 0:EventLogEntryType.Error
/// 1:EventLogEntryType.FailureAudit
/// 2:EventLogEntryType.Information
/// 3:EventLogEntryType.SuccessAudit
/// 4:EventLogEntryType.Warning
/// </param>
/// <returns>EventLogEntryType</returns>
public static EventLogEntryType getEventLogEntryType(int typeNum)
{
EventLogEntryType type = EventLogEntryType.Information;
switch (typeNum)
{
case 0:
type = EventLogEntryType.Error;
break;
case 1:
type = EventLogEntryType.FailureAudit;
break;
case 2:
type = EventLogEntryType.Information;
break;
case 3:
type = EventLogEntryType.SuccessAudit;
break;
case 4:
type = EventLogEntryType.Warning;
break;
default:
break;
}
return type;
}
#endregion
#region 写入事件日志
/// <summary>
/// 写入事件日志
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="message">写入EventLog的信息</param>
/// <param name="type">EventLogEntryType</param>
/// <param name="eventID">事件的应用程序特定标识符</param>
/// <param name="category">与消息关联的应用程序特定子类别</param>
/// <param name="rawData">包含与此项关联的二进制数据的字节数组</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriteEntry(string logName, string sourceName, string message, EventLogEntryType type, int eventID, short category, byte[] rawData)
{
if (!EventLog.SourceExists(sourceName))
EventLog.CreateEventSource(sourceName, logName);
else
{
if (EventLogClass.LogNameFromSourceName(sourceName).ToLower() != logName.ToLower())
{
EventLogClass.DeleteEventSource(sourceName);
EventLogClass.CreateEventSource(logName, sourceName);
}
}
EventLog.WriteEntry(sourceName, message, type, eventID, category, rawData);
}
/// <summary>
/// 写入事件日志
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="message">写入EventLog的信息</param>
/// <param name="type">EventLogEntryType</param>
/// <param name="eventID">事件的应用程序特定标识符</param>
/// <param name="category">与消息关联的应用程序特定子类别</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriteEntry(string logName, string sourceName, string message, EventLogEntryType type, int eventID, short category)
{
if (!EventLog.SourceExists(sourceName))
EventLog.CreateEventSource(sourceName, logName);
else
{
if (EventLogClass.LogNameFromSourceName(sourceName).ToLower() != logName.ToLower())
{
EventLogClass.DeleteEventSource(sourceName);
EventLogClass.CreateEventSource(logName, sourceName);
}
}
EventLog.WriteEntry(sourceName, message, type, eventID, category);
}
/// <summary>
/// 写入事件日志
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="message">写入EventLog的信息</param>
/// <param name="type">EventLogEntryType</param>
/// <param name="eventID">事件的应用程序特定标识符</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriteEntry(string logName, string sourceName, string message, EventLogEntryType type, int eventID)
{
if (!EventLog.SourceExists(sourceName))
EventLog.CreateEventSource(sourceName, logName);
else
{
if (EventLogClass.LogNameFromSourceName(sourceName).ToLower() != logName.ToLower())
{
EventLogClass.DeleteEventSource(sourceName);
EventLogClass.CreateEventSource(logName, sourceName);
}
}
EventLog.WriteEntry(sourceName, message, type, eventID);
}
/// <summary>
/// 写入事件日志
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="message">写入EventLog的信息</param>
/// <param name="type">EventLogEntryType</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriteEntry(string logName, string sourceName, string message, EventLogEntryType type)
{
if (!EventLog.SourceExists(sourceName))
EventLog.CreateEventSource(sourceName, logName);
else
{
if (EventLogClass.LogNameFromSourceName(sourceName).ToLower() != logName.ToLower())
{
EventLogClass.DeleteEventSource(sourceName);
EventLogClass.CreateEventSource(logName, sourceName);
}
}
EventLog.WriteEntry(sourceName, message, type);
}
/// <summary>
/// 写入事件日志
/// </summary>
/// <param name="logName">日志名称</param>
/// <param name="sourceName">事件源名称</param>
/// <param name="message">写入EventLog的信息</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriteEntry(string logName, string sourceName, string message)
{
if (!EventLog.SourceExists(sourceName))
EventLog.CreateEventSource(sourceName, logName);
else
{
if (EventLogClass.LogNameFromSourceName(sourceName).ToLower() != logName.ToLower())
{
EventLogClass.DeleteEventSource(sourceName);
EventLogClass.CreateEventSource(logName, sourceName);
}
}
EventLog.WriteEntry(sourceName, message);
}
#endregion
#region 删除日志
/// <summary>
/// 删除日志
/// </summary>
/// <param name="logName">日志名</param>
/// <param name="machineName">计算机名,本地计算机则为"."</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Delete(string logName, string machineName)
{
EventLog.Delete(logName, machineName);
}
/// <summary>
/// 删除日志
/// </summary>
/// <param name="logName">日志名</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void Delete(string logName)
{
EventLogClass.Delete(logName, ".");
}
#endregion
#region 获取指定的源注册到的日志名称
/// <summary>
/// 获取指定的源注册到的日志名称
/// </summary>
/// <param name="source">事件源</param>
/// <param name="machineName">计算机名,本地为""</param>
/// <returns>日志名称</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public static string LogNameFromSourceName(string source, string machineName)
{
return EventLog.LogNameFromSourceName(source, machineName);
}
/// <summary>
/// 获取指定的源注册到的本地的日志名称
/// </summary>
/// <param name="source">事件源</param>
/// <returns>日志名称</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public static string LogNameFromSourceName(string source)
{
return EventLogClass.LogNameFromSourceName(source, ".");
}
#endregion
#region 获取事件日志的数组
/// <summary>
/// 获取指定计算机上的事件日志的数组
/// </summary>
/// <param name="machineName">计算机名</param>
/// <returns>EventLog[]</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public static EventLog[] GetEventLogs(string machineName)
{
return EventLog.GetEventLogs(machineName);
}
/// <summary>
/// 获取本地事件日志的数组
/// </summary>
/// <returns>EventLog[]</returns>
[MethodImpl(MethodImplOptions.Synchronized)]
public static EventLog[] GetEventLogs()
{
return EventLog.GetEventLogs();
}
#endregion
#region 从事件日志中移除应用程序的事件源注册
/// <summary>
/// 从事件日志中移除应用程序的事件源注册
/// </summary>
/// <param name="source">事件源</param>
/// <param name="machineName">计算机名</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void DeleteEventSource(string source, string machineName)
{
EventLog.DeleteEventSource(source, machineName);
}
/// <summary>
/// 从事件日志中移除应用程序的事件源注册
/// </summary>
/// <param name="source">事件源</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void DeleteEventSource(string source)
{
EventLog.DeleteEventSource(source);
}
#endregion
}
}
Logging项目-->WriterTextLog文件
using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.CompilerServices;
using System.Threading;
using Microsoft.Win32;
using System.IO;
namespace Logging
{
public class WriterTextLog
{
/// <summary>
/// 写入信息到文本文件
/// </summary>
/// <param name="FilePath">文件路径</param>
/// <param name="Message">写入消息</param>
[MethodImpl(MethodImplOptions.Synchronized)]
public static void WriterLog(string FilePath, string Message)
{
//如果文件不存在,则创建
//注意:日志文件一天一个
string FilePathName = FilePath + DateTime.Today.ToString("yyyyMMdd") + ".log";
string MessageString = DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss") + " " + Message;
try
{
if (!File.Exists(FilePathName))
{
using (FileStream fs = File.Create(FilePathName))
{
byte[] info = new UTF8Encoding(true).GetBytes(MessageString + "/r/n");
fs.Write(info, 0, info.Length);
}
}
else
{
using (StreamWriter _MyStream = new StreamWriter(FilePathName, true))
{
_MyStream.WriteLine(MessageString);
//_MyStream.WriteLine("------------------------");
}
}
}
catch
{
}
}
}
}