如何检查程序是否在兼容模式下运行并在标题栏中显示模式

时间:2013-04-20 17:02:09

标签: c#

我的程序需要在XP兼容模式下运行。我需要一种方法来检查是否在XP兼容模式下运行。如果没有告诉用户在兼容模式下运行它。我搜索过但没有找到答案。我试过这个this.Text = System.Environment.OSVersion.VersionString;,但它返回Microsoft Windows NT 6.1.7601 Service Pack 1而不是模拟的XP。

(编辑)

经过更多搜索和dll导入没有任何工作正常,它将始终返回Microsoft Windows NT 6.1.7601 Service Pack 1.所以我创建了一个控制台加载来处理注册表然后运行我的程序。

using System;
using System.Security.Permissions;
using Microsoft.Win32;

[assembly: RegistryPermissionAttribute(SecurityAction.RequestMinimum, ViewAndModify = "HKEY_CURRENT_USER")]

class RegKey
{
    static void Main()
    {
        int Major = Environment.OSVersion.Version.Major;
        int Minor = Environment.OSVersion.Version.Minor;

        if (Major == 5 && Minor == 1)
        {   
            System.Diagnostics.Process.Start(System.IO.Directory.GetCurrentDirectory() + "\\MyApp.exe");
        }
        else if (Major > 5)
        {
            if (System.IO.File.Exists(System.IO.Directory.GetCurrentDirectory() + "\\MyApp.exe"))
            {
                RegistryKey Rkey = Registry.CurrentUser.CreateSubKey("SOFTWARE\\Microsoft\\Windows NT\\CurrentVersion\\AppCompatFlags\\Layers");
                //Creates Name And Value
                Rkey.SetValue(System.IO.Directory.GetCurrentDirectory() + "\\MyApp.exe", "WINXPSP3");
                //Starts App.
                 System.Diagnostics.Process.Start(System.IO.Directory.GetCurrentDirectory() + "\\MyApp.exe", "Load");

                //Waites 500 milliseconds so my program can get the registry info
                //(Not needed any more) 
                //System.Threading.Thread.Sleep(500);

                //Deletes Name And Value
                //(Handled by MyApp.exe to increase load time)
                //Rkey.DeleteValue(System.IO.Directory.GetCurrentDirectory() + "\\MyApp.exe");
            }
            else
            {
                Console.WriteLine("File Not Found!!!");
                Console.WriteLine("( MyApp.exe ) Must Be In Same Directory As The Loader.exe.");
                Console.ReadLine();
            }
        }
    }
}

我以这种方式处理注册表,因此我的程序可以移植,并防止指向任何内容的注册表项。我支持他们是更好的方法来获得我想要的结果。

(编辑2)

经过更多检查后,我只需要在64位系统上运行XP兼容模式。 32位系统不需要在XP兼容模式下运行。我用平台x86编译了我的应用程序。所以它必须与64位系统读取注册表的方式有关。

0 个答案:

没有答案