Windows 11 添加使用IE浏览器

下载附件中的 InternetExplorer.zip 后,解压得到 InternetExplorer.exe,将其放置在 C:\Program Files\Internet Explorer 或 C:\Program Files (x86)\Internet Explorer 目录,执行 InternetExplorer /i 后,在当前用户的桌面会生成 Internet Explorer 的图标,双击此图标,即可正常使用 Internet Explorer

const string ProductName = "InternetExplorer";
const string Prefix_HTTPS = "https://";
const string Prefix_HTTP = "http://";

// 检查命令行参数是否包含 /i
if (args.Length > 0 && args[0].Equals("/i", StringComparison.OrdinalIgnoreCase))
{
    CreateShortcut();
    return;
}

try
{
    var url = GetStartPage(args);
    try
    {
        OpenInternetExplorer(url);
    }
    catch
    {
        TryKillInternetExplorer();
        OpenInternetExplorer(url);
    }
}
catch (Exception ex)
{
    MessageBox.Show(ex.ToString(), ProductName, MessageBoxButtons.OK, MessageBoxIcon.Error);
}

static void CreateShortcut()
{
    try
    {
        // 获取当前程序的目录
        string exeDirectory = AppDomain.CurrentDomain.BaseDirectory;
        string exePath = Path.Combine(exeDirectory, "InternetExplorer.exe");

        // 获取当前用户桌面路径
        string desktopPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        string shortcutLocation = Path.Combine(desktopPath, "Internet Explorer.url");

        // 检查桌面上是否已经存在相同名称的快捷方式,如果存在则删除它
        if (File.Exists(shortcutLocation))
        {
            File.Delete(shortcutLocation);
        }

        // 创建新的快捷方式
        using (StreamWriter writer = new StreamWriter(shortcutLocation))
        {
            writer.WriteLine("[InternetShortcut]");
            writer.WriteLine([        DISCUZ_CODE_82        ]quot;URL=file:///{exePath.Replace('\\', '/')}");
            writer.WriteLine("IconIndex=0");

            // 使用当前目录下iexplore.exe的图标
            string iconPath = Path.Combine(exeDirectory, "iexplore.exe");
            writer.WriteLine([        DISCUZ_CODE_82        ]quot;IconFile={iconPath.Replace('\\', '/')}");
        }
    }
    catch (Exception)
    {
    }
}

static bool IsHttpUrl([NotNullWhen(true)] string? url, bool httpsOnly = false) => url != null &&
       (url.StartsWith(Prefix_HTTPS, StringComparison.OrdinalIgnoreCase) ||
             (!httpsOnly && url.StartsWith(Prefix_HTTP, StringComparison.OrdinalIgnoreCase)));

static bool IsFileUrl(string? url)
{
    try
    {
        return File.Exists(url!);
    }
    catch
    {

    }
    return false;
}

static string GetStartPage(string[] args)
{
    var url = GetArgument(args, 0);
    var httpsOnly = GetArgumentB(args, 1);
    if (url != null && (IsHttpUrl(url, httpsOnly) || IsFileUrl(url)))
    {
        return url;
    }

    try
    {
        if (Environment.Is64BitOperatingSystem)
        {
            url = GetStartPageByRegistry(RegistryView.Registry64);
            if (url != null && IsHttpUrl(url, httpsOnly))
            {
                return url;
            }
        }
        url = GetStartPageByRegistry(RegistryView.Registry32);
        if (url != null && IsHttpUrl(url, httpsOnly))
        {
            return url;
        }
    }
    catch
    {
    }

    return "https://www.baidu.com";
}

static string? GetStartPageByRegistry(RegistryView registryView)
{
    using var registryKey = RegistryKey.OpenBaseKey(RegistryHive.CurrentUser, registryView)
        .OpenSubKey(@"Software\Microsoft\Internet Explorer\Main");
    return registryKey.GetValue("Start Page")?.ToString();
}

static string? GetArgument(string[] args, int index)
{
    try
    {
        return args[index];
    }
    catch
    {
        return null;
    }
}

static bool GetArgumentB(string[] args, int index, bool defaultValue = false)
{
    try
    {
        return bool.Parse(args[index]);
    }
    catch
    {
    }
    return defaultValue;
}

static void OpenInternetExplorer(string url)
{
    var IE = new InternetExplorer
    {
        Visible = true
    };
    IE.Navigate(url);
}

static void TryKillInternetExplorer()
{
    var processes = Process.GetProcessesByName("iexplore");
    foreach (var process in processes)
    {
        try
        {
            process.Kill();
        }
        catch
        {

        }
    }
}

 

点击下载InternetExplorer.zip