relative path problem in C#. how to fix?

Hi there;

I have got a C# program that I need run an executable and the executable in a folder in my project, but I cannot reach it, other than absolute path. How can I do this in relative path?

Kind regards.
LVL 12
jazzIIIloveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

käµfm³d 👽Commented:
but I cannot reach it, other than absolute path
Have you changed the current working directory of your application in code? That might contribute to a relative path not functioning correctly.
0
jazzIIIloveAuthor Commented:
I haven't got a code for the relative path as I don't know.

Soln
- Proj
- - Properties
- - References
- - Folder1
- - - A file
- - Folder2
- - - Another file
- - Program.cs
- - Form.cs
- - sometextfile.txt

Here is the hierarchy; I need from Form.cs to reach A file, Another file and sometextfile.txt

There must be a way to reach those files apart from absolute path. How can I do it without too much stripping/trimming?

Kind regards.
0
käµfm³d 👽Commented:
Have you set each "A File", "sometextfile.txt", and "Another file"  "Copy to Ouput Directory" property to true? If you do not, those files (and folders) will not be copied to the bin directory when you build, and you would at that point have to refer to the "source" file (the one you see in the Solution Explorer) directly. This may or may not be desirable. If you "copy to output", then you can have a default version of the file that gets copied each build--this would replace any changes you made to the output file during execution.
untitled.PNG
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

käµfm³d 👽Commented:
Have you set each "A File", "sometextfile.txt", and "Another file"  "Copy to Ouput Directory" property to true?
Sorry, I should have said, "'Copyt to Output Directory' property to "Copy Always" or "Copy if Newer."
0
Maulik ModiCommented:
Assuming your C# Program is console or Winforms then your path to executable would be relative to BIN\Debug directory of your C# program

e.g. C:\\MyProject\\Myfolder\\myExecutable.exe

See if this helps - Application.StartupPath
http://social.msdn.microsoft.com/Forums/en-US/vbgeneral/thread/e763ae8c-1284-43fe-9e55-4b36f8780f1c/
0
jazzIIIloveAuthor Commented:
I mean I want to give my solution to a friend of mine, that's the reason, it should be portable.

Kind regards.
0
käµfm³d 👽Commented:
That's fine, but are you distributing the text files as well? Even if you are not going to and you are going to let your friend create his own, once you set the relative path for the file, it must exist in that same hierarchical structure on either machine. If you have the files on a folder named "TextFiles" that is in the same directory as the main executable, then your friend needs to have a directory named "TextFiles" that is in the same directory as the main executable.

"TextFiles" is just an example.
0
jazzIIIloveAuthor Commented:
OK, for copy thing, it sounds easy but as you see I got 2 folders and my code is referenced those folders too, but those folders have no such Copy option.

Regards.
0
käµfm³d 👽Commented:
but those folders have no such Copy option.
If you correctly set the "Copy to Output Directory" property on any file in each of those directories, then the folder should automatically be created. You will need to set the property's value for each file that you want in the output folder.
0
jazzIIIloveAuthor Commented:
You are right, it's working. To populate the answer i really want to know doing this programmatically. I mean, is there some kind of static variable checking and setting this automatically?

Kind regards.
0
käµfm³d 👽Commented:
To populate the answer i really want to know doing this programmatically.
Forgive me, but I'm not entirely sure what you mean. Can you provide an example scenario?
0
jazzIIIloveAuthor Commented:
I mean how can i reach the files/folders apart from Copying to Debug directory but stg like that;

myObj. Goup2levels();

Stg like that.

Kind regards and thanks for your patience.
0
käµfm³d 👽Commented:
I mean how can i reach the files/folders apart from Copying to Debug directory but stg like that;
The only thing the "Copy to Debug" is doing for you at this point is creating the structure and adding the files in the appropriate places. You don't have to enable this option--you can go to your bin\Debug folder and do "New Folder" if you like. You use your relative paths in your code like you seem to already be doing. You (and your friend) just need to create the appropriate structure to mimic what your code expects. If you did something like this in code:

System.Diagnostics.Process.Start("libraries\\run_me_app.exe", "my arguments");

Open in new window


and you haven't changed your application's working directory in code:

e.g.

// NOT done something like this
System.IO.Directory.SetCurrentDirectory("..\\..\..");

Open in new window


then all you have to do is make sure that you have a "libraries" folder inside the same directory that your executable is in, and that you have a file called "run_me_app.exe" in the "libraries" folder. Here's a screenshot of what I mean:
untitled.PNG
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
jazzIIIloveAuthor Commented:
I see, that's the thing i was asking but is there stg like Application.Somefunctiontosearchtheabovehierarchy.DoThis().DoThat()

Or Program. Somefunctiontosearchtheabovehierarchy()

Or Environment. Somefunctiontosearchtheabovehierarchy

In the above code, you seem to copy the things manually.

Regards.
0
käµfm³d 👽Commented:
Somefunctiontosearchtheabovehierarchy
I'm trying to work out exactly what you are after, so please bear with my thick-headedness today  = )

Would be referring to behavior similar to what you get with the PATH environment variable, where you can type an executable's name rather than its full path if it is located in one of the folders specified within the PATH variable?
0
jazzIIIloveAuthor Commented:
yes! bulls eye!
0
käµfm³d 👽Commented:
OK. Are you wanting to store this as an actual environment variable, similar to PATH, or did you plan on storing it in some other manner?
0
jazzIIIloveAuthor Commented:
Hi there;

Ah, the issue my scope is unlimited. We can try both of them.

Kind regards and thanks for your patience.
0
jazzIIIloveAuthor Commented:
Any updates?

Regards.
0
sarabandeCommented:
i don't know whether i have understood your requirements but using a relative path is easy. your program would reach A file by "..\\..\\Folder1\\A File" and Another File by "..\\..\\Folder2\\Another File". that nothing has to do with functionality as in PATH environment but simply is relative path syntax where one period means current folder . and two periods .. means parent folder. instead of \\ you also could use / .

if your files could be either in Folder1 or Folder2 you could check with File.Exists function whether it is the one or the other. File.Exists also takes a relative or absolute path.

Sara
 
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
.NET Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.