/// <summary>
/// 生成文件到临时目录
/// </summary>
private static bool WriteFileToTempDir(string LoaclFilePath, string BindingName, string ApploctionName, out string error)
{
error = "";
FileStream FileCreate = null;
byte[] buffer = null;
// @"D:/TestUrl/URl.config";
try
{
//读取文件
FileCreate = File.OpenRead(LoaclFilePath);
buffer = new byte[FileCreate.Length];
FileCreate.Read(buffer, 0, buffer.Length);
FileCreate.Close();
//模版内容
string TemplateText = Encoding.UTF8.GetString(buffer, 0, buffer.Length);
//程序的路径
string AppLocalPath = Path.Combine(Directory.GetCurrentDirectory(), ApploctionName).Replace("//", "");
//临时目录的路径
string TempRegisterPath = Path.Combine(Environment.GetEnvironmentVariable("Temp") + @"/", "Temp_Register.reg");
//模版替换
TemplateText = TemplateText.Replace("%SysName%", BindingName).Replace("%ProgrammePath%", AppLocalPath);
//FileStream _FileCreate = null;
StreamWriter FileStreamWrite = null;
FileCreate = new FileStream(TempRegisterPath, FileMode.Create);
FileStreamWrite = new StreamWriter(FileCreate, System.Text.Encoding.Unicode);
//开始写入
FileStreamWrite.Write(TemplateText);
//清空缓冲区
FileStreamWrite.Flush();
//关闭流
FileStreamWrite.Close();
FileCreate.Close();
//"/s " +
//暂停100毫秒。如程序运行太快无法注册
Thread.Sleep(100);
Process.Start("regedit.exe", "/s " + TempRegisterPath);
Thread.Sleep(100);
//删除临时注册文件
File.Delete(TempRegisterPath);
}
catch (System.Exception ex)
{
error="系统信息写入注册表发生错误";
}
return string.IsNullOrEmpty(error);
}