2

My program need to be run in XP compatible mode. I need A way to check if is running in XP compatible mode. If not tell the user to run it in compatible mode. I have searched but found no answer. I have tried this this.Text = System.Environment.OSVersion.VersionString; but it returns Microsoft Windows NT 6.1.7601 Service Pack 1 not the emulated XP.

(Edit)

After more searches and dll imports nothing worked right it would always returns Microsoft Windows NT 6.1.7601 Service Pack 1. So I created A console loaded to handle registry then run my program.

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

I handle registry in this way so my program can be portable and prevent registry entries that point to nothing. I am shore that they are better ways to get the results I was looking for.

(Edit 2)

After more checking I only have to run XP compatible mode on 64 bit systems. 32 bit systems do not need to run in XP compatible mode. I compiled my app with platform x86. So it must has something to do with the way 64 bit systems read the registry.

Danny
  • 29
  • 2
  • 4
    Hard to imagine why you'd need to run in compat mode. What does your app do wrong? – David Heffernan Apr 20 '13 at 17:04
  • You'll need to use unmanaged code to do this but if your app is doing something that requires Compat Mode you're likely already doing that somewhere else :) – Michael Edenfield Apr 20 '13 at 17:09
  • Please have a look at the following question that i noticed on SO [Is a Program Running in Compatibility Mode](http://stackoverflow.com/a/3445031/1012641) – Patrick D'Souza Apr 20 '13 at 17:33
  • 1
    [Application compatibility layers are there for the customer, not for the program](http://blogs.msdn.com/b/oldnewthing/archive/2010/03/11/9976571.aspx): "it's there to clean up after your mistakes, not to let you hide behind them." – Damien_The_Unbeliever Apr 24 '13 at 13:48
  • My app uses parts form an older app. If my app is not run in XP compatible mode it doesn't work right. It is not my app that has the problem. – Danny Apr 24 '13 at 14:05
  • Yes, 64-bit systems do not store files in the same place in the registry as 32-bit applications. You should ask a new question about how to fix your app, rather than hacking around it. Disabling registry redirection might be an option. – Cody Gray - on strike Apr 24 '13 at 23:05

0 Answers0