Exemplo n.º 1
0
        public void SynchronizeCsproj()
        {
            string             projectDirectory = Path.GetDirectoryName(solution);
            ProjectRootElement root             = ProjectRootElement.Open(solution.Replace(".sln", ".csproj"));

            foreach (var itemGroup in root.ItemGroups)
            {
                if (itemGroup.Condition.Length != 0)
                {
                    continue;
                }

                foreach (var item in itemGroup.Items.ToArray())
                {
                    if (item.ItemType == "Compile")
                    {
                        // We need to use WebUtility.UrlDecode because paths in .csproj are URL encoded from some reason
                        if (!File.Exists(Path.Combine(projectDirectory, WebUtility.UrlDecode(item.Include))))
                        {
                            itemGroup.RemoveChild(item);
                        }
                    }
                }
            }

            foreach (string filePath in Directory.EnumerateFiles(projectDirectory, "*.*", SearchOption.AllDirectories))
            {
                if (Path.GetExtension(filePath) == ".cs")
                {
                    string relativePath = filePath.RelativeToPath(projectDirectory);

                    root.AddItemChecked("Compile", relativePath);
                }
            }

            root.Save();
        }