C ා foundation - how to find the path location of devenv

Keywords: C# REST

I. Preface

VS2017 has been installed since this year. Sometimes script compilation is needed. However, MS no longer supports VS2015's "VS140comtools% vsvars32. Bat" in Script Compilation on VS2017. I am really satisfied. No way, I can always use devenv, so I wrote a program to get the latest version of VS. There are many online moves, such as what vswhere, what absolute path to judge, etc. I think I'd better take the registry as a breakthrough.

 

Two, code

var hasVS = false;
var registryPath = @"SOFTWARE\Wow6432Node\Microsoft\VisualStudio\SxS\VS7";
var localMachineRegistry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, Environment.Is64BitOperatingSystem ? RegistryView.Registry64 : RegistryView.Registry32);
var vsPaths = ReadRegistryInfo(localMachineRegistry, registryPath);
var highestVSdevenvPath = string.Empty;
if (vsPaths != null && vsPaths.Any())
{
    var tempVersion = 0;
    foreach (KeyValuePair<string, string> kvp in vsPaths)
    {
          var devenvExePath = Path.Combine(kvp.Value, @"Common7\IDE\devenv.exe");
          if (File.Exists(devenvExePath))
          {
              var currentVersion = Convert.ToInt32(kvp.Key.Split('.')[0]);
              if (currentVersion > tempVersion)
              {
                   tempVersion = currentVersion;
                   highestVSdevenvPath = devenvExePath;
              }
           }
    }

    if (!string.IsNullOrEmpty(highestVSdevenvPath))
    {
          hasVS = true;
    }
}


//Read Registry Info
public Dictionary<string, string> ReadRegistryInfo(RegistryKey registryKey, string registryInfoPath)
{
    if (registryKey == null || string.IsNullOrEmpty(registryInfoPath)) return null;
    try
    {
         RegistryKey rsg = registryKey.OpenSubKey(registryInfoPath, false);
         if (rsg != null)
         {
             var keyNameArray = rsg?.GetValueNames();
             var result = new Dictionary<string, string>();
             foreach (var name in keyNameArray)
             {
                  string keyValue = (string)rsg.GetValue(name);
                  result.Add(name,keyValue);
             }
             rsg.Close();
             return result;
         }
         return null;
   }
   catch
   {
         return null;
    }
}

 

Found devenv.exe, then the rest of the things are easy to do, make a C ා compilation confusion packaging gadget appropriate.

 

Three, ending

This may be the last blog that I wrote in my old employer's time. Next month, I will go to a famous design institute to be the first software engineer in a certain field in a center. In the past two years, I have learned a lot in the company. Teachers from Autodesk have trained me to be a full stack engineer. Thank them! My industry circle is very small. It will be a long time in the future. Maybe we will get together again one day. Bless my colleagues and leaders, thank them!

Posted by changeback on Mon, 30 Mar 2020 08:59:05 -0700