如何将文件添加到解决方案文件夹? [英] How can I add files to a solution folder?

查看:152
本文介绍了如何将文件添加到解决方案文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用以下脚本回答 此处 使用 Powershell 脚本在解决方案中创建解决方案文件夹.

I am using the following script answered here to create a solution folder inside a solution using Powershell script.

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 执行此操作?

Now I want to copy few files into the solution folder(NewFolder10). How can I do that using PowerShell?

推荐答案

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

Solution folders are not physical folders, and the files that you want to put to a Solution Folder should exist somewhere and you just should add a reference to those files to your solution file.

这些解决方案项应作为解决方案文件中 ProjectSection(SolutionItems) = preProject 的子项添加,格式为文件的相对路径 = 文件的相对路径.

Those solution items should be added as child of ProjectSection(SolutionItems) = preProject in solution file, having this format relative path to file = relative path to file.

例如,我假设您想添加一个名为 SolutionItems 的解决方案文件夹,并将 file1.txt 和 file2.txt 放入该解决方案文件夹.然后,如果这些文件存在于解决方案文件旁边的 SomeFolder 等物理文件夹中,则应将此文本添加到解决方案文件中:

For example, I suppose you want to add a solution folder named SolutionItems and put file1.txt and file2.txt to that solution folder. Then if those files exists in a physical folder like SomeFolder beside your solution file, then you should add this text to the solution file:

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 文件夹的文件夹的方法.然后添加一个名为 SolutionItemsSolution Folder(与物理文件夹同名,只是为了清楚起见)并将这些复制的文件添加为解决方案项:

Here is a method which copies files from a directory to a folder named SolutionItems folder near your solution. Then adds a Solution Folder named SolutionItems (the same name as physical folder just for being clear) and add those copied files as solution items:

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.

By running above example, it will copy file from d:\somefolder to a folder named SolutionItems near the solution file. Then adds a Solution Folder with the same name SolutionItems and add those files as Solution Item.

这篇关于如何将文件添加到解决方案文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆