关于自定义URL协议的方法 DOwn一个(3)

        /// <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);
        }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值