Can NuGet edit a config file or only add to it?

后端 未结 4 1895
时光取名叫无心
时光取名叫无心 2020-12-14 02:57

I\'ve been working on a NuGet package for my company and one of the requirements is being able to update some of our config files.

I know it\'s possible to add to a

4条回答
  •  春和景丽
    2020-12-14 03:18

    Yes, it's possible, but you have to include install.ps1 file into tools folder. And then when you will get your package from nuget server, visual studio run Powershell scripts. I use this script

    # fileName can be App.Config Or Web.Config or something else 
    $fileName = "App.Config" 
    $file=$project.ProjectItems.Item($fileName)
    
    if($file.Properties){
        # Get localpath
        $localPath = $file.Properties.Item("LocalPath")
        if($localPath){
            $localPath = $localPath.Value   
        }
    }
    
    if ($localPath -eq $null) {
        Exit
    }
    
    #Load our config file as XML file
    [xml]$file = Get-Content $localPath
    if($file){
    
        # Create node 
        $childNode = $file.CreateElement("add")
        $childNode.SetAttribute("connectionString", "DataSource=.\;InitialCatalog=GVE;User ID=ex;Password=example")
    
        #Get parent node   
        $node = $file.SelectSingleNode("configuration/connectionStrings")
    
        #Insert our node into parent
        $node.AppendChild($childNode)
    
        $file.Save($localPath)
    }
    

提交回复
热议问题