📄 systemmaintain.cs
字号:
using System;
namespace Utilities
{
/// <summary>
/// 维护系统的功能集中于此。
/// </summary>
public class SystemMaintain
{
private static string filePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
/// <summary>
/// 备份系统文件
/// </summary>
public static void BackupSystemFile()
{
try
{
//备份数据库
CopyFile(filePath + @"\infocenter.mdb", filePath + @"\Backup\infocenter.mdb");
//备份树
CopyFile(filePath + @"\MainTree.xml", filePath + @"\Backup\MainTree.xml");
//备份配置文件
if(System.IO.File.Exists(filePath + @"\PersonalInfo.cfg"))
{
CopyFile(filePath + @"\PersonalInfo.cfg", filePath + @"\Backup\PersonalInfo.cfg");
}
}
catch(Exception ex)
{
throw new Exception("备份数据时失败," + ex.Message);
}
}
/// <summary>
/// 复制文件,如文件在目的地已存在,则删除之
/// </summary>
/// <param name="sourceFileName"></param>
/// <param name="destFileName"></param>
public static void CopyFile(string sourceFileName, string destFileName)
{
try
{
if(System.IO.File.Exists(destFileName))//删除已有的文件
{
System.IO.File.Delete(destFileName);
}
else if(!System.IO.Directory.Exists(filePath + "\\Backup"))
{
System.IO.Directory.CreateDirectory(filePath + "\\Backup");
}
System.IO.File.Copy(sourceFileName, destFileName);
}
catch(Exception ex)
{
throw new Exception("操作文件:" + sourceFileName + "---" + destFileName + "时失败,系统给出的信息为:" + ex.Message);
}
}
/// <summary>
/// 还原系统文件
/// </summary>
public static void RestoreSystemFile()
{
string filePath = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location);
try
{
//复原数据库
CopyFile(filePath + @"\Backup\infocenter.mdb", filePath + @"\infocenter.mdb");
//还原树
CopyFile(filePath + @"\Backup\MainTree.xml", filePath + @"\MainTree.xml");
//还原配置文件
if(System.IO.File.Exists(filePath + @"\Backup\PersonalInfo.cfg"))
{
CopyFile(filePath + @"\Backup\PersonalInfo.cfg", filePath + @"\PersonalInfo.cfg");
}
}
catch(Exception ex)
{
throw new Exception("还原数据失败," + ex.Message);
}
}
/// <summary>
/// 压缩和修复数据库
/// </summary>
/// <param name="connectionString">用来连接到Access数据库</param>
/// <param name="DBFileName">要压缩的MDB文件的全名(路径+文件名)</param>
public static void CompactAndRepairDB(string connectionString, string DBFileName)
{
DBFileName = filePath + @"\" + DBFileName;
if(!System.IO.File.Exists(DBFileName))
{
throw new ApplicationException("找不到文件:" + DBFileName);
}
string backupFileName = DBFileName + ".bak";
try
{
object[] oParams;
//create an inctance of a Jet Replication Object
object objJRO = Activator.CreateInstance(Type.GetTypeFromProgID("JRO.JetEngine"));
//filling Parameters array
//cnahge "Jet OLEDB:Engine Type=5" to an appropriate value
// or leave it as is if you db is JET4X format (access 2000,2002)
//(yes, jetengine5 is for JET4X, no misprint here)
oParams = new object[] {connectionString, "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + backupFileName +";Jet OLEDB:Engine Type=5"};
//invoke a CompactDatabase method of a JRO object
//pass Parameters array
objJRO.GetType().InvokeMember("CompactDatabase", System.Reflection.BindingFlags.InvokeMethod, null, objJRO, oParams);
//database is compacted now
//to a new file backupFileName
//let's copy it over an old one and delete it
System.IO.File.Delete(DBFileName);
System.IO.File.Move(backupFileName, DBFileName);
//clean up (just in case)
System.Runtime.InteropServices.Marshal.ReleaseComObject(objJRO);
objJRO=null;
}
catch(Exception ex)
{
throw new ApplicationException("在压缩和修复数据库时出错,系统给出的信息为:" + ex.Message);
}
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -