【问题标题】:What is the correct way to save and edit a file in C# while using an .nsi install script?使用 .nsi 安装脚本时在 C# 中保存和编辑文件的正确方法是什么?
【发布时间】:2015-08-07 15:06:13
【问题描述】:

在我的项目中,我有一个 xml 文件,其中存储了程序读取和显示的一堆字符串。我想要发生的是使用该文件构建程序,使用安装脚本创建安装程序(我已经通过构建后事件将所有 .dll 和 .exe 文件移动到脚本的 bin 文件夹)然后拥有最终用户能够访问该文件并让程序在每次运行时自动加载该文件。

这是我目前打开文件的方式。这看起来在调试文件夹中,但我希望它位于持久位置。

XmlDocument doc = new XmlDocument();
doc.Load("PowerManagers.xml");

我的 .nsi 文件:

!define FullName "Power Manager Safety Testing"
!define ProductName "Power Manager Safety Testing"
!define ProductExe "PowerManagerSafetyTesting.exe"

;--------------------------------
;Include Modern UI

!include "MUI.nsh"
!include "Framework .Net Install.nsh"
!include "Common Functions.nsh"
!include "Common Drivers.nsh"

ShowInstDetails show

!insertmacro Insert_System_Configuration

;--------------------------------
; The stuff to install
Section "${ProductName}" Sec_Id_Main

SectionIn RO
Call MainSectionInstall

; The files to use in this installer
File "..\..\bin\PowerManagerSafetyTesting\*.exe"
File "..\..\bin\PowerManagerSafetyTesting\*.dll"
File "..\..\bin\PowerManagerSafetyTesting\PowerManagers.xml"

SectionEnd

;---------


;---------

; Optional section (can be disabled by the user)
Section "Start Menu Shortcuts" Sec_Id_Start

Call StartMenuSection

SectionEnd

;---------

; Optional section (can be disabled by the user)
Section "Desktop Shortcuts" Sec_Id_Desktop

Call DesktopShortcutsSection

SectionEnd

;--------------------------------
;   Uninstaller

Section "Uninstall"

Call un.UninstallerSection

SectionEnd


;--------------------------------
;  Page descriptions
!insertmacro MUI_FUNCTION_DESCRIPTION_BEGIN
!insertmacro MUI_DESCRIPTION_TEXT ${SECDOTNET_ID} $(DESC_LONGDOTNET)
!insertmacro MUI_DESCRIPTION_TEXT ${Sec_Id_Main} "Installs the core files     required to run the ${ProductName}"
!insertmacro MUI_DESCRIPTION_TEXT ${Sec_Id_Start} "Installs short cuts to the start menu"
!insertmacro MUI_DESCRIPTION_TEXT ${Sec_Id_Desktop} "Installs short cuts to the desktop"
!insertmacro MUI_FUNCTION_DESCRIPTION_END

【问题讨论】:

    标签: c# nsis


    【解决方案1】:

    您需要将您的 XML 文件放在 Windows 数据目录中,即C:\ProgramData\Your_App,然后在您的 C# 应用程序中从那里读取,然后从您的安装程序将文件写入该路径中。

    C# 程序

    String strPath = Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData);
    strPath += "\\Your_App\\PowerManagers.xml";
    
    XmlDocument doc = new XmlDocument();
    doc.Load(strPath);
    

    NSIS 脚本

    ReadRegStr $0 HKLM "Software\Microsoft\Windows\CurrentVersion\Explorer\ShellFolders" "Common AppData"
    CreateDirectory "$0\Your_App"
    SetOutPath "$0\Your_App"
    File "..\..\bin\PowerManagerSafetyTesting\PowerManagers.xml"
    

    【讨论】:

    • 我使用,这种注册表读取方法用于我的脚本,它在 XP、7、8 上运行良好。如果您有任何代码以外的使用插件,请分享。
    • 谷歌“shellfolders oldnewthing”。阅读文档中的 NSIS 常量部分,了解如何正确执行...
    • @Anders:我没有发现任何有用的东西。你能给我链接或样本吗?渴望知道适用于 XP、7、8 的 AppData 的 NSIS 常量。
    • @hypeni: SetShellVarContext all + $AppData
    猜你喜欢
    • 2018-04-04
    • 1970-01-01
    • 2016-04-14
    • 1970-01-01
    • 2017-04-24
    • 2015-04-01
    • 2010-10-26
    • 1970-01-01
    • 2019-11-11
    相关资源
    最近更新 更多