如何使用 MSIX“Windows 应用程序打包项目”设置应用程序的“开始于”文件夹

问题描述 投票:0回答:1

我有一个应用程序从其自己的程序文件文件夹中读取文件;应用程序假定它是在该特定文件夹(本地当前目录)中启动的。 (目前它崩溃了,因为需要该文件,但找不到)。

当前目录:C:\Windows\system32

读取文件 f.dat 时出错

使用其他安装程序(如

Wix
InnoSetup
)可以创建快捷方式及其特定的
start in
文件夹。

如何解决这个问题?是否可以使用 Visual Studio

start in
设置应用程序的
Windows Application Packaging Project
文件夹,它会生成
MSIX
安装程序。

我正在考虑在运行时确定安装文件夹,然后使用绝对路径读取文件。 也许我应该查询一些记录的注册表项。

windows-store msix windows-application-packaging
1个回答
0
投票

解决方案是不依赖“start in”文件夹,而是使用运行进程的路径。

在我的具体情况下,我使用

Go
(Golang)。使用 os.Executable()

这是我的旧代码,在使用

MSIX
时失败:

// read using current directory 
cwd, _ := os.Getwd()
log.Printf("current directory: %s", cwd)

b, err := os.ReadFile(fName)
if err != nil {
    log.Fatal("error reading file %s", fName)
}
return b

新代码:

// read using starting process directory 
exePath, err := os.Executable()
if err == nil {
    // strip executable name from path 
    exePath = strings.ReplaceAll(exePath, "AppName.exe", "")
    fullPath := fmt.Sprintf("%s%s", exePath, fName)

    // read file
    log.Printf("reading: %s\n", fullPath)
    b, err := os.ReadFile(fullPath)
    if err != nil {
        log.Printf("failed reading %s : %v\n",fName, err)
    }
    return b
}
© www.soinside.com 2019 - 2024. All rights reserved.