Jump to content

Refering to external files without using full path

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
5 replies to this topic

#1
Zeddan

Zeddan

    Newbie

  • Members
  • PipPip
  • 11 posts
I have an application that is connected to some external sources (text and image files). I refer to these sources through their full path. The sources are located in the same folder as the application (the executable).

Whenever I move the folder which contains the application and the sources to another directory, the sources are (for natural reasons) unreachable if I don't change the path in the source code.

I would like to know if there is a way to point directly to the containing folder of the application and the sources so I (or someone else) can run the application correctly from whatever directory without having to change the path in the source code or moving the folder to a specific directory.

Any suggestions are appreciated.

#2
Zero-P

Zero-P

    Newbie

  • Members
  • Pip
  • 5 posts
I believe there is a function called "GetModuleFileName" or something like that. (I don't use it much and I don't have a reference in front of me.... so someone else might clarify). It will return the path of where your exe is. Then do a reverse search on that path until you reach a "\" character. Chop off the rest and add on the relative file names:

Ie:
CString GetMyFilePath(CString relative)
{
  CString path;
  GetModuleFileName(path.GetBuffer(256);
  int pos = path.ReverseFind('\\');
  
  return path.Left(pos+1) + relative;
}

Something like that should probably get what you want...

Edited by Jaan, 23 October 2008 - 08:39 PM.
Please use code tags when you're posting your codes!


#3
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
you could also do
string path;
path = Application.StartupPath + "\\";
It will set it as the executables path, without the executable's filename in it and then add a \ onto it.

Edited by Termana, 25 October 2008 - 04:05 AM.
Added Code into [CODE][/CODE]


#4
rashed2007

rashed2007

    Newbie

  • Members
  • Pip
  • 3 posts
I would like to know if there is a way to point directly to the containing folder of the application and the sources so I (or someone else) can run the application correctly from whatever directory without having to change the path in the source code or moving the folder to a specific directory.

#5
Termana

Termana

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 4,057 posts
If I understand what you've just said rashed2007 - aren't you just asking the same question that the opening poster asked? (that has been answered btw)

Interested in participating in community events?
Want to harness your programming skill and turn it into absolute prowess?
Come join our programming events!


#6
Xav

Xav

    Writes binary right handed and hex left handed

  • Members
  • PipPipPipPipPipPipPipPipPip
  • 13,118 posts

Termana said:

you could also do

string path;

path = Application.StartupPath + "\\";

It will set it as the executables path, without the executable's filename in it and then add a \ onto it.

Lol just put it all on one line!


string path = Application.StartupPath + "\\";


Jordan said:

Good members, like yourself, stick around and post for ages to come!
Mr. Xav | Blog | Forums