首页 新闻 会员 周边 捐助

winform重启程序

0
悬赏园豆:20 [已解决问题] 解决于 2014-01-08 17:32

在修改当前 用户密码后,程序重启,我用的是Application.Restart(),调试程序木有问题,可以重启。但是后来在网上看到另一种重启方式

System.Diagnostics.Process.Start(Application.ExecutablePath);
Application.Exit();

大虾们,有没有知道这两个的区别的?小的菜鸟一枚,好想知道有什么区别

你好,再见的主页 你好,再见 | 初学一级 | 园豆:105
提问于:2013-12-20 15:37
< >
分享
最佳答案
0

有区别,区别在于:

[SecurityPermission(SecurityAction.Demand, Flags = SecurityPermissionFlag.UnmanagedCode), SecurityPermission(SecurityAction.LinkDemand, Flags = SecurityPermissionFlag.UnmanagedCode)]
public static void Restart()
{
    if (Assembly.GetEntryAssembly() == null)
    {
        throw new NotSupportedException(SR.GetString("RestartNotSupported"));
    }
    bool flag = false;
    Process currentProcess = Process.GetCurrentProcess();
    if (string.Equals(currentProcess.MainModule.ModuleName, "ieexec.exe", StringComparison.OrdinalIgnoreCase))
    {
        string str = string.Empty;
        new FileIOPermission(PermissionState.Unrestricted).Assert();
        try
        {
            str = Path.GetDirectoryName(typeof(object).Module.FullyQualifiedName);
        }
        finally
        {
            CodeAccessPermission.RevertAssert();
        }
        if (string.Equals(str + "\\ieexec.exe", currentProcess.MainModule.FileName, StringComparison.OrdinalIgnoreCase))
        {
            flag = true;
            Application.ExitInternal();
            string text = AppDomain.CurrentDomain.GetData("APP_LAUNCH_URL") as string;
            if (text != null)
            {
                Process.Start(currentProcess.MainModule.FileName, text);
            }
        }
    }
    if (!flag)
    {
        if (ApplicationDeployment.IsNetworkDeployed)
        {
            string updatedApplicationFullName = ApplicationDeployment.CurrentDeployment.UpdatedApplicationFullName;
            uint hostTypeFromMetaData = (uint)Application.ClickOnceUtility.GetHostTypeFromMetaData(updatedApplicationFullName);
            Application.ExitInternal();
            UnsafeNativeMethods.CorLaunchApplication(hostTypeFromMetaData, updatedApplicationFullName, 0, null, 0, null, new UnsafeNativeMethods.PROCESS_INFORMATION());
            return;
        }
        string[] commandLineArgs = Environment.GetCommandLineArgs();
        StringBuilder stringBuilder = new StringBuilder((commandLineArgs.Length - 1) * 16);
        for (int i = 1; i < commandLineArgs.Length - 1; i++)
        {
            stringBuilder.Append('"');
            stringBuilder.Append(commandLineArgs[i]);
            stringBuilder.Append("\" ");
        }
        if (commandLineArgs.Length > 1)
        {
            stringBuilder.Append('"');
            stringBuilder.Append(commandLineArgs[commandLineArgs.Length - 1]);
            stringBuilder.Append('"');
        }
        ProcessStartInfo startInfo = Process.GetCurrentProcess().StartInfo;
        startInfo.FileName = Application.ExecutablePath;
        if (stringBuilder.Length > 0)
        {
            startInfo.Arguments = stringBuilder.ToString();
        }
        Application.ExitInternal();
        Process.Start(startInfo);
    }
}
收获园豆:10
Launcher | 高人七级 |园豆:45050 | 2013-12-20 16:07

好森奥的赶脚,能否汉字解释一下

你好,再见 | 园豆:105 (初学一级) | 2013-12-20 16:46

俺是菜鸟,有些不懂

你好,再见 | 园豆:105 (初学一级) | 2013-12-20 16:47

@你好,再见: 那没办法了,你去找本教程读读吧。

Launcher | 园豆:45050 (高人七级) | 2013-12-20 16:50
其他回答(2)
0

 if (text != null)
            {
                Process.Start(currentProcess.MainModule.FileName, text);
            }

看楼上的代码 restart里面也调用了这个Start方法 没什么区别吧

收获园豆:5
wolfy | 园豆:2636 (老鸟四级) | 2013-12-20 16:12

你需要仔细读代码,这段代码中有很多坑,对运行环境考虑不周全时就会写出没有这些坑的代码。

支持(0) 反对(0) Launcher | 园豆:45050 (高人七级) | 2013-12-20 16:32
0

带参数的怎么办?

比如:

myapp.exe -s -t -url http://www.google.com/ -test -other -all

收获园豆:5
undefined | 园豆:898 (小虾三级) | 2013-12-20 16:33

额外补充:刚查了下, .net framework 1.1里面是不包括这个方法的. 所以很有可能那是一段老代码.

支持(0) 反对(0) undefined | 园豆:898 (小虾三级) | 2013-12-20 16:35
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册