开发者

Storing settings for a nuget package. Is there a better way?

I have a console application that is available via nuget or on it's own. It gets installed into the tools directory for the NuGet package. The application requires 3 pieces of 'configuration' information.

  • a database connection string
  • a folder path
  • one more configuration option (string)

Currently, I store these configuration values in a text file right next to the exe in a file called settings.js, serialized as json.

When the application first runs, if the file is not present, it creates one with default values.

I keep the settings.js file in this location so the file will get checked into source co开发者_开发百科ntrol.

My question is about maintaining the settings file across versions. If you Update-Package via nuget, everything works great, except the new version doesn't have any settings i had configured, because there is a new folder created for the new version.

I have written a powershell script to run in init.ps1 to pull the settings from the last version of the package, and seems to work. However this feels kinda dirty and I am wondering if there is a better way to solve this problem when using nuget to deliver my application.

param($installPath, $toolsPath, $package)
Set-Alias hump (Join-Path $toolsPath hump.exe)

$sorted_list = new-object system.collections.SortedList
$parent_path = Join-Path $installPath ".."
foreach($f in Get-ChildItem $parent_path -Filter Humpback* | Foreach {$_.FullName}){
    $sorted_list.Add($f,$f)
}
if($sorted_list.Count -gt 1){
    $old_path = $sorted_list.Values[$sorted_list.Count - 2]
    $new_path = Join-Path $installPath "tools"
    $current_settings = Join-Path $new_path "settings.js"
    $has_current_settings = Test-Path $current_settings
    if($has_current_settings -eq $false){
        $old_settings = Join-Path $old_path "tools\settings.js"
        Copy-Item $old_settings  $new_path
    }
}

Also, init.ps1 doesn't appear to run when installing a package using the command line tool (nuget.exe). Is this expected behavior?


Can you access System.Environment.GetFolderPath? I'd just create a folder under ApplicationData special folder, and store the settings there.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜