我正在創建一個應用程序。要更改多個項目的名稱空間(解決方案),我將內容複製到另一個位置,更改文件夾名稱,替換文件內容中的文本(使用新名稱空間的舊名稱空間),一切正常。該解決方案具有文件夾(這些文件夾只存在於溶液中,VS),每個文件夾中包含的項目和文件......這是原來的(或模板)解決方案的結構:ProjectGuid(.csproj),如何正確生成它?
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
|
正如你看到的,項目出現瞭解決方案的文件夾,althoug文件夾維持其內容的他人和解決方案編譯。 我使用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);
}
我需要你的幫助。