I have an application which reads a file from its own programs files folder; The application assumes it get's started in that specific folder, the local current directory. (Currently it crashes, as the file is required, and not found).

Current directory: C:\Windows\system32

Error reading file f.dat

Using other Installers like Wix and InnoSetup one can create the shortcuts, with their specific start in folders.

How do solve this? Is it possible to set the start in folder for the Application, using the Visual Studio Windows Application Packaging Project, which generates the MSIX installer.

I am thinking of determining the install folder at runtime, and then to read the file using an absolute path. Perhaps there is some documented registry entry I should query.

1

There are 1 best solutions below

0
On

The solution is to not rely on the "start in" folder, but to use the path of the running process.

In my specific case I am using Go (Golang). Using os.Executable()

Here is my old code which failed when using 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

And the new code:

// 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
}