C# Basics - How to find the path location of devenv

I. Introduction

       I started to install VS2017 this year. Sometimes I need to use script compilation. However, MS no longer supports the "%VS140COMNTOOLS%vsvars32.bat" of VS2015 in the script compilation on VS2017. I am really convinced. Then there is no way, I can always use devenv, so I wrote a program to get the devenv of the latest version of VS. There are also quite a lot of tricks on the Internet, what vswhere, what to judge the absolute path, and so on. I think I'd better use the registry as a breakthrough.

 

2. 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;
    }
}

 

Find devenv.exe, then the rest of the things are easy to do, it is appropriate to do a C# compilation, confusion and packaging gadget.

 

3. The end

       This may be the last blog I wrote when I was at the old club. Next month, I will go to a famous design institute to be the first software engineer in a certain field in a certain center. In the past two years, I have learned a lot in the company. The masters from Autodesk have cultivated me into a full-stack engineer. I really thank them! My industry circle is very small, and it will be a long time in Japan. Maybe one day we will meet again. Bless my colleagues and leaders, thank them!

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324929935&siteId=291194637