C# 自主更新

发布于 2024-09-30 17:35:47 字数 1340 浏览 0 评论 0原文

在我的新项目(windows-C#-vs2008)中,我希望可执行文件能够自动更新自身 - 也许来自网络服务器 在这里,我所做的是将任何更新的 dll 下载到“隔离存储”(每个作为单独的 dll),然后修改 CurrentDomain_AssemblyResolve() 方法

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

,以便它首先检查其隔离存储中是否存在库,然后再从可执行文件加载它。 但是如果我想更改主 exe 中的某些内容该怎么办? [我尝试创建一个加载程序项目(控制台 ap),从中调用我的主程序(将输出更改为 dll),但是这次程序集解析事件没有触发,因为引用 dll 是在新加载程序的 bin 文件夹下生成的编译期间的项目] 任何帮助将不胜感激..

public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            try
            {
                string assemName = new AssemblyName(args.Name).Name;

                object i_StoreRootDir = i_StorageFile.GetType().GetField
                    ("m_RootDir", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(i_StorageFile);

                string i_StorePath = CombinePaths(i_StoreRootDir.ToString(), I_STOREDIR, assemName, DLL_EXT);

                if (File.Exists(i_StorePath))
                {
                    return Assembly.LoadFrom(i_StorePath);
                }
                else
                {
                    //load it from resource.
                    return null;
                }
            }
            catch (Exception)
            {
                throw;
            }

in my new project (windows-C#-vs2008)I want the executable to be able to autonomously update itself- perhaps from a network server
Here What I do is download any updated dlls to "isolated storage" (each as separate dlls) then modify the CurrentDomain_AssemblyResolve() method

AppDomain.CurrentDomain.AssemblyResolve += new ResolveEventHandler(CurrentDomain_AssemblyResolve);

such that it first checks for the presence of library in its isolated storage before loading it from executable .
But what if I want to change something in the main exe.
[I tried creating a loader project(console ap) from which, it calls my main program(changed output as dll) ,however this time the assembly resolve event is not getting trigger because the reference dlls are geting generated under bin folder of new loader project during compilation ]
Any help would be highly appreciated..

public static Assembly CurrentDomain_AssemblyResolve(object sender, ResolveEventArgs args)
        {
            try
            {
                string assemName = new AssemblyName(args.Name).Name;

                object i_StoreRootDir = i_StorageFile.GetType().GetField
                    ("m_RootDir", BindingFlags.Instance | BindingFlags.NonPublic).GetValue(i_StorageFile);

                string i_StorePath = CombinePaths(i_StoreRootDir.ToString(), I_STOREDIR, assemName, DLL_EXT);

                if (File.Exists(i_StorePath))
                {
                    return Assembly.LoadFrom(i_StorePath);
                }
                else
                {
                    //load it from resource.
                    return null;
                }
            }
            catch (Exception)
            {
                throw;
            }

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

绿光 2024-10-07 17:35:47

您可以查看ClickOnce 部署作为框架的一部分。您还可以查看wyUpdate。还有BitsUpdater

You may take a look at ClickOnce deployment as part of the framework. You may also checkout wyUpdate. There's also BitsUpdater.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文