【问题标题】:How can I add files to a solution folder?如何将文件添加到解决方案文件夹?
【发布时间】:2017-12-04 15:57:48
【问题描述】:

我正在使用回答 here 的以下脚本在使用 Powershell 脚本的解决方案中创建解决方案文件夹。

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 做到这一点?

【问题讨论】:

    标签: visual-studio powershell visual-studio-2017 solution


    【解决方案1】:

    解决方案文件夹不是物理文件夹,您要放入解决方案文件夹的文件应该存在于某处,您只需将这些文件的引用添加到您的解决方案文件中。

    这些解决方案项应作为ProjectSection(SolutionItems) = preProject 的子项添加到解决方案文件中,格式为relative path to file = relative path to file

    例如,我假设您想添加一个名为 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
    

    示例

    这是一种将文件从目录复制到解决方案附近名为SolutionItems 文件夹的方法。然后添加一个名为SolutionItems解决方案文件夹(为了清楚起见,与物理文件夹同名)并将这些复制的文件添加为解决方案项目:

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

    这是用法:

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

    通过运行上面的示例,它将文件从d:\somefolder 复制到解决方案文件附近名为SolutionItems 的文件夹中。然后添加一个 Solution Folder 同名 SolutionItems 并将这些文件添加为 Solution Item

    【讨论】:

    • 这会将文件从d:\somefolder复制到SolutionItems文件夹?
    • 是的。它将文件从d:\somefolder 复制到解决方案文件附近名为SolutionItems 的文件夹。然后添加一个同名的 Solution Folder SolutionItems 并将这些文件添加为 Solution Item
    • 非常感谢 :)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-06-14
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多