c# - ProjectGuid(.csproj),如何正确生成?

标签 c# visual-studio msbuild guid projects-and-solutions

我正在创建一个应用程序。要更改多个项目的命名空间(解决方案),我将内容复制到另一个位置,更改文件夹名称,替换文件内容中的文本(旧命名空间与新命名空间),一切都可以。解决方案有文件夹(这些文件夹只存在于解决方案中,在 VS 中)并且每个文件夹都包含项目和文件......这是原始(或模板)解决方案的结构:

    MySolution
    |
    |---SolutionFolder1
    |           |
    |           |-- Documentation.docx
    |
    |---SolutionFolder2
    |           |
    |           |-- ThirdPartyLibrary.dll
    |
    |---SolutionFolder3
    |           |
    |           |-- MySolution.MyLibrary(**Project**)
    |                     |
    |                     |-- Class.cs
    |
    |---SolutionFolder4
    |           |
    |           |-- MySolution.MyConsoleApp(**Project**)
    |                     |
    |                     |-- Program.cs
    |

我只是将整个解决方案复制到另一个地方,甚至在 .csproj 和 .sln 文件中更改了一些名称。如果我打开新的解决方案,所有内容都出现在它的位置(如上面的结构),解决方案文件夹中的项目和解决方案编译,一切正常。 但是,如果我在打开新解决方案时更改每个项目的 ProjectGuid,我会得到以下结构:

    MySolution
    |
    |---SolutionFolder1
    |           |
    |           |-- Documentation.docx
    |
    |---SolutionFolder2
    |           |
    |           |-- ThirdPartyLibrary.dll
    |
    |---SolutionFolder3          
    |
    |---SolutionFolder4
    |
    |
    |-- MySolution.MyLibrary(**Project**)
    |           |
    |           |-- Class.cs
    |
    |-- MySolution.MyConsoleApp(**Project**)
    |           |
    |           |-- Program.cs
    |

如您所见,项目出现在解决方案文件夹之外,尽管其他文件夹保留其内容并且解决方案编译。 我使用 Guid.NewGuid() 生成新的 ProjectGuid 并更新其他 .csproj 和 .sln 文件。 这是我的代码(此方法复制每个文件的内容):

//the projectGuidCollection contains a collection of instances like this:
ProjectGuid proj = new ProjectGuid()
{
    FilePath = @"c:\newpath\to\MyLibrary.csproj",
    OldGuid = "D4BD0EB3-8B59-4A33-B8A3-D549065EEC6D", //<ProjectGuid>{}</ProjectGuid>
    NewGuid = Guid.NewGuid().ToString()
};

public static void Replace(string filePath, string oldNamespace, string newNamespace, 
                           IEnumerable<ProjectGuid> projectGuidCollection)
{
    string text = File.ReadAllText(filePath, Encoding.UTF8);
    text = text.Replace(oldNamespace, newNamespace);

    List<ProjectGuid> allProjectGuids = new List<ProjectGuid>(projectGuidCollection);
    if (filePath.EndsWith(".csproj"))
    {
        ProjectGuid currentProject = allProjectGuids.Find(o => { return o.FilePath.Equals(filePath); });
        if (currentProject != null)
        {
            List<ProjectGuid> otherProjs = allProjectGuids.FindAll(o => { return !o.FilePath.Equals(filePath); });

            //changes current ProjecGuid:
            text = text.Replace(currentProject.OldGuid, currentProject.NewGuid.ToUpper());

            //change other projectguids (references):
            foreach (var refProject in otherProjs)
            {
                text = text.Replace(refProject.OldGuid, refProject.NewGuid);
            }
        }
    }

    //update new projectguids in solution file:
    if (filePath.EndsWith(".sln"))
    {
        foreach (var p in allProjectGuids)
        {
            text = text.Replace(p.OldGuid.ToUpper(), p.NewGuid.ToUpper());
        }
    }

    File.WriteAllText(filePath, text,Encoding.UTF8);
}

我需要你的帮助。

最佳答案

我花了很多时间搜索信息后,终于找到了解决方案 here在@MarkLakata 的评论中

You will probably want to do System.Guid.NewGuid().ToString("B").ToUpper() if you want to be compatible with some MS Build tools that can't understand lower case UUIDs. For example, vdproj setup projects have UUIDs in upper case and will throw an exception it you give it lower case. –

所以解决方案是:

//the projectGuidCollection contains a collection of instances like this:
ProjectGuid proj = new ProjectGuid()
{
    FilePath = @"c:\newpath\to\MyLibrary.csproj",
    OldGuid = "D4BD0EB3-8B59-4A33-B8A3-D549065EEC6D", //<ProjectGuid>{}</ProjectGuid>
    NewGuid = Guid.NewGuid().ToString("B") // <--- HERE THE SOLUTION!!
};

嗯,就是这样。

关于c# - ProjectGuid(.csproj),如何正确生成?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29839957/

相关文章:

c# - 将 INotifyPropertyChanged 添加到模型?

c# - 在没有第 3 方库的情况下通过 API 1.1 使用 Twitter OAuth

javascript - Microsoft Jscript Angular 未定义

c++ - 赋值语句截断字符

.net - 在没有 Visual Studio 2010 的 Build Machine 中通过 MSbuild 发布

.net - 在构建时注入(inject)程序集版本号

c# - csc2.exe 退出,代码为 1

c# - 可以通过查询结果获取结果集行数吗?

c# - .net 核心跨平台桌面应用程序

c# - AssemblyVersion 和 AssemblyFileVersion 在一个单独的文件中