2017-12-04 5 views
2

Powershellスクリプトを使用してソリューション内にソリューションフォルダを作成するには、hereという次のスクリプトを使用しています。ソリューションフォルダにファイルを追加するにはどうすればよいですか?

Function AddFolderToSolution($folderName, $solutionFile) 
{ 
    $solutionFolderGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}" 
    $content = [System.IO.File]::ReadLines($solutionFile) 
    $lines = New-Object System.Collections.Generic.List[string] 
    $lines.AddRange($content) 
    $index = $lines.IndexOf("Global") 
    $guid = [System.Guid]::NewGuid().ToString().ToUpper() 
    $txt = "Project(`"$solutionFolderGuid`") = `"$folderName`", `"$folderName`", `"$guid`"" 
    $lines.Insert($index, $txt) 
    $lines.Insert($index+1, "EndProject") 
    [System.IO.File]::WriteAllLines($solutionFile, $lines) 
} 

AddFolderToSolution "NewFolder10" "D:\Solution1.sln" 

解決策フォルダ(NewFolder10)にファイルをほとんどコピーしません。 PowerShellを使ってどうすればいいですか?

答えて

1

ソリューションフォルダは物理フォルダではなく、ソリューションフォルダに配置するファイルはどこかに存在する必要があり、これらのファイルへの参照をソリューションファイルに追加するだけです。

これらのソリューション項目は、という形式のソリューションファイルにProjectSection(SolutionItems) = preProjectの子として追加する必要があります。

たとえば、SolutionItemsという名前のソリューションフォルダを追加し、そのソリューションフォルダにfile1.txtとfile2.txtを追加するとします。そして、それらのファイルは、あなたはソリューションファイルにこのテキストを追加する必要があり、あなたのソリューションファイルの横にSomeFolderのような物理的なフォルダに存在する場合:

Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "SolutionItems", "SolutionItems", "B73A0302-952E-42D6-925D-ABCB95684477" 
    ProjectSection(SolutionItems) = preProject 
     ..\SomeFolder\file1.txt = ..\SomeFolder\file1.txt 
     ..\SomeFolder\file2.txt = ..\SomeFolder\file2.txt 
    EndProjectSection 
EndProject 

ここでAからそのファイルをコピーします方法であり、ソリューションの近くにあるSolutionItemsという名前のフォルダに移動します。そして、ソリューションフォルダという名前SolutionItems(単に明確さのための物理的なフォルダと同じ名前)を追加し、ソリューションアイテムとしてそれらのコピーされたファイルを追加します。

AddFolderToSolution "d:\somefolder" "d:\mysolution\Solution1.sln" 

:ここ

Function AddFolderToSolution($folderPath, $solutionFile) 
{ 
    $solutionPath = Split-Path -Parent -Path $solutionFile 
    $folderName = "SolutionItems" 
    $destination = Join-Path $solutionPath $folderName 
    $solutionFolderGuid = "{2150E333-8FDC-42A3-9474-1A3956D46DE8}" 
    $content = [System.IO.File]::ReadLines($solutionFile) 
    $lines = New-Object System.Collections.Generic.List[string] 
    $lines.AddRange($content) 
    $index = $lines.IndexOf("Global") 
    $guid = [System.Guid]::NewGuid().ToString().ToUpper() 
    $range = New-Object System.Collections.Generic.List[string] 
    $range.Add(
     "Project(`"$solutionFolderGuid`") = `"$folderName`", `"$folderName`", `"$guid`"") 
    $range.Add("`tProjectSection(SolutionItems) = preProject") 
    New-Item -Path $destination -ItemType Directory -Force 
    Get-ChildItem -Path $folderPath -File | Copy-Item -Destination $destination -Force 
    Get-ChildItem -Path $destination -File| ForEach-Object { 
     $itemName = Split-Path -Leaf -Path $_.FullName 
     $range.Add("`t`t..\$folderName\$itemName = ..\$folderName\$itemName") 
    } 
    $range.Add("`tEndProjectSection") 
    $range.Add("EndProject") 
    $lines.InsertRange($index, $range) 
    [System.IO.File]::WriteAllLines($solutionFile, $lines) 
} 

と使用方法です上記の例を実行すると、d:\somefolderからソリューションファイルの近くのSolutionItemsという名前のフォルダにファイルがコピーされます。次にソリューションフォルダと同じ名前のSolutionItemsを追加し、これらのファイルをソリューションアイテムとして追加します。

+0

これは 'd:\ somefolder'から' SolutionItems'フォルダにファイルをコピーしますか? – ANIL

+1

はい。 'd:¥somefolder'からソリューションファイルの近くの' SolutionItems'という名前のフォルダにファイルをコピーします。次に、「SolutionItems」という名前の* Solution Folder *を追加し、それらのファイルを* Solution Item *として追加します。 –

+0

ありがとうございました:) – ANIL

関連する問題