0

So I know how to make it, I just want it to open the file without specifying the path.

For example: I have it in

C:\Users\\(me)\Desktop\Projects\BCs\BSCV2\bin\Debug\BSC.exe

but if I give it to a friend, he has a different username, (him) for example, so the command won't be able to execute even if he has it on his desktop because the path isn't valid anymore.

Here's a part of the code:

#include <iostream>
#include <windows.h>
using namespace std;

int main()
{
    int a;
    cout << endl;
    cout << " This window is used for launching the programs." << endl;
    cout << " Type in the number of the program you want to use and press Enter." << endl;
    cout << endl;
    cout << " 1) BSCV2 << endl;
    cout << endl;
    cout << " "; cin >> a; cout << endl;
    cout << endl;

        if (a == 1){
            system ("start C:\\Users\\(me)\\Desktop\\Projects\\BCs\\BSCV2\\bin\\Debug\\BSCV2.exe");
            system ("pause");
        }

    return 0;
}

How can I make it run on anyone's PC, regardless of where they put it? Also, if you could re-write my code as an example, I'd appreciate it.

Spikatrix
  • 20,225
  • 7
  • 37
  • 83
VeeZee
  • 9
  • 2

1 Answers1

0

You will need to get the "home" directory for the current user logged in. Reference this post: How to get the current user's home directory in Windows, or this one: How can I find the user's home dir in a cross platform manner, using C++?

However, are you absolutely sure that all users (on their respective machines) running your application will have the exact directory path to the executable you're trying to call (\Desktop\Projects\BCs\BSCV2\bin\Debug\BSCV2.exe)?

You may be better off writing a function to search for the executable, or ask the user to specify where it is.

Community
  • 1
  • 1
AdeelMufti
  • 341
  • 1
  • 8