简体   繁体   中英

ProjectGuid (.csproj), how to generate it correctly?

I'm creating an app. to change the namespace of multiple projects (solution), I copy the content to another location, change folder names, replace text in files content (old namespaces with new namespaces), all is OK. The solution has folders(these folders only exist in the solution, in VS) and every folder contains projects, and files...This is the structure of the original (or template) solution:

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

I only copy the whole solution to another place, changing some names even in .csproj and .sln files. If I open the new solution all appears in its place (like above structure), projects inside solution folder an the solution compiles, all is OK. But if I change the ProjectGuid of each project when I open the new solution i get this structure:

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

As you see, projects appear out of solution folders, althoug the others folders maintain its content and the solution compile. I use Guid.NewGuid() to generate the new ProjectGuid and I update the others .csproj and the .sln file. This is my code (this method copies the content of every file):

//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);
}

I need your help.

After I've spent many hours searching for info, finally i've found the solution here in the @MarkLakata's comment

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.

So the solution is:

//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!!
};

Well, that is all.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM