I wrote a simple function to get all property name from a msbuild target file.  Such as visual studio’s Microsoft.WebApplication.targets file in direcotry of “%ProgramFiles%\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications\”.  It checks for possible property name case mismatch as well.  The properNames in the target file is in format of $(xxx).  So the function here is really simple.

 

        private string GetTargetsFromFile(string filePath)

        {

            if (!System.IO.File.Exists(filePath))

            {

                MessageBox.Show(String.Format("{0} does not exist!", filePath));

                return;

            }

            string content = System.IO.File.ReadAllText(filePath);

            string properties = "";

 

            Dictionary<string, string> PropertyDict = new Dictionary<string, string>();

            Dictionary<string, string> NoCasePropertyDict = new Dictionary<string, string>();

 

            //find all of the $(propertyName)

            Regex r = new Regex(@"\$\((.*?)\)");

 

            MatchCollection mc;

 

            mc = r.Matches(content);

            // Loop through the match collection to retrieve all

            // matches and positions.

            for (int i = 0; i < mc.Count; i++)

            {

                string value = mc[i].Value.Substring(2, mc[i].Value.Length - 3);

                // Add the match string to the string array.

                if (!PropertyDict.Keys.Contains(value))

                {

                    if (NoCasePropertyDict.Keys.Contains(value.ToUpperInvariant()))

                    {

                        properties += "Warning, non-matching name (already defind before) found in character location of " + mc[i].Index.ToString() + ", its substring is \r\n";

                        properties += content.Substring(mc[i].Index, 200) + "\r\n\r\n";

                    }

                    else

                    {

                        NoCasePropertyDict.Add(value.ToUpperInvariant(), "");

                        PropertyDict.Add(value, "");

                    }

                }

                // Record the character position where the match was found.

                //matchposition[i] = mc[i].Index;  

            }

 

            foreach (string value in PropertyDict.Keys)

            {

                properties += "        Private m_" + value + " As String = \"" + value + "\"\r\n";

            }

 

            Return properties;

        }

    }

 

我写了一个简单的函数,可以将msbuild的目标文件(target)的所有属性名称提出。如在“%ProgramFiles%\MSBuild\Microsoft\VisualStudio\v9.0\WebApplications\”目录下的visual studio文件 Microsoft.WebApplication.targets。函数检查可能的属性名称大小写不一致的问题。在目标文件中,属性名称的格式是$(xxx),所以相关的函数很简单。