Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 844
AiApaec Avatar asked Apr 24 '15 06:04

AiApaec


People also ask

What is project GUID in Visual Studio?

The project GUID in the solution is a unique identifier of a project across the solution inside VS. In non-SDK-based projects, it's a required element that must be present in the project, and if not, will re-added the first time you open the project in Visual Studio.

How do I change my project GUID in Visual Studio?

Open the copied project file (. csproj) and replace the project GUID with the new one. Then open the copied solution file (. sln) and replace all occurrences of the original GUID with the new one.

How do Csproj files work?

CSPROJ files are used by Visual Studio to store references to items within a project and to store compilation options. The CSPROJ file, saved in XML format, stores all the references for your project including your compilation options.


1 Answers

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.

like image 148
AiApaec Avatar answered Oct 14 '22 23:10

AiApaec