简体   繁体   中英

Relative paths not working if batch file calls exe based on OS architecture

I have a requirement where I need to run a particular .exe based on the architecture.

My folder structure is like this: 主文件夹层次结构

The Tools folder looks like this 工具文件夹

Under that the Binaries folder contains 2 sub folder (one each for 32/64 bit) 二进制

Each of these folders (x64/x32) looks like below: X64文件夹(同样是x32文件夹)

The Root folder contains the .bat (start.bat) file that calls the appropriate exe (For example: if it is 32bit, it calls "\\Binaries\\x86\\Tool.exe" or if it is 64bit, it calls "\\Binaries\\x64\\Tool.exe".

The code in start.bat is as below:

@ECHO OFF
if exist "%SYSTEMDRIVE%\Program Files (x86)\" (
   start "" /d "%~dp0" "Binaries\x86\Tool.exe"
) else (
   start "" /d "%~dp0" "Binaries\x64\Tool.exe"
)

The calling is fine and it calls the particular .exe. The problem comes when the .exe application tries to use the XML file (each folder also contains a XML file, parameters.xml, along with exe) it throws an error. I am accessing the XML file using relative paths like ("./parameters.xml").

I tried recoding the code by using "System.AppDomain.CurrentDomain.BaseDirectory" (as this is a WPF exe). That works for relative path, but another scenario fails. I will explain the same below:

In the application i am getting the instances of SQL installed on the machine. To achieve that I am using the following code:

internal static List < string > SQLServerInstances() {
    var sqlInstances = new List < string > ();

    try {
        using(RegistryKey sqlKey = RegistryKey.OpenRemoteBaseKey(RegistryHive.LocalMachine, "").
        OpenSubKey(@
        "SOFTWARE\Microsoft\Microsoft SQL Server\Instance Names\SQL")) {
            if (sqlKey != null) {
                foreach(string versionKeyName in sqlKey.GetValueNames()) {
                    sqlInstances.Add(versionKeyName);
                }
            }
        }
    } catch (Exception de) {
        throw de;
    }
}

This code returns no instances if I use the batch file to run the .exe. If I run the .exe directly from "Binaries\\x64\\Tool.exe", this code passes and returns me the SQL instances properly.

I am not sure what the issue is. This might be expected behaviour but seems a bit weird.

Try changing your start.bat code this way:

@ECHO OFF
cd "%~dp0"
if exist "%SYSTEMDRIVE%\Program Files (x86)\" (
   start "" "Binaries\x86\Tool.exe"
) else (
   start "" "Binaries\x64\Tool.exe"
)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM