![](/img/trans.png)
[英]Is the value for ProjectGuid in a .csproj file supposed to be unique?
[英]ProjectGuid (.csproj), how to generate it correctly?
我正在创建一个应用程序。 要更改多个项目的名称空间(解决方案),我将内容复制到另一个位置,更改文件夹名称,替换文件内容中的文本(旧名称空间与新名称空间),一切正常。 解决方案有文件夹(这些文件夹只存在于解决方案中,在VS中),每个文件夹都包含项目和文件......这是原始(或模板)解决方案的结构:
MySolution
|
|---SolutionFolder1
| |
| |-- Documentation.docx
|
|---SolutionFolder2
| |
| |-- ThirdPartyLibrary.dll
|
|---SolutionFolder3
| |
| |-- MySolution.MyLibrary(**Project**)
| |
| |-- Class.cs
|
|---SolutionFolder4
| |
| |-- MySolution.MyConsoleApp(**Project**)
| |
| |-- Program.cs
|
我只将整个解决方案复制到另一个地方,甚至在.csproj和.sln文件中更改了一些名称。 如果我打开新解决方案都出现在它的位置(如上面的结构),解决方案文件夹里面的项目解决方案编译,一切都OK。 但是,如果我在打开新解决方案时更改每个项目的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);
}
我需要你的帮助。
我花了寻找信息多小时后,终于我找到了解决这里的@ MarkLakata的评论
你可能想要做System.Guid.NewGuid()。ToString( “B” )。ToUpper()如果你想与一些无法理解小写UUID的MS Build工具兼容。 例如,vdproj安装项目的UUID大写为大写,并会抛出一个异常,你给它小写。 -
所以解决方案是:
//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!!
};
嗯,就是这样。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.