Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

relative path problem in C#. how to fix?

Posted on 2011-10-03
23
Medium Priority
?
1,798 Views
Last Modified: 2013-11-26
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.
0
Comment
Question by:jazzIIIlove
20 Comments
 
LVL 75

Assisted Solution

by:käµfm³d 👽
käµfm³d   👽 earned 1000 total points
ID: 36905402
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36905426
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36905493
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
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36905498
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
 
LVL 6

Assisted Solution

by:Maulik Modi
Maulik Modi earned 500 total points
ID: 36905500
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36905515
I mean I want to give my solution to a friend of mine, that's the reason, it should be portable.

Kind regards.
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36905543
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36905548
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36905568
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36906688
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36910931
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36911180
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
 
LVL 75

Accepted Solution

by:
käµfm³d   👽 earned 1000 total points
ID: 36911294
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36911579
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
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36912598
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36913463
yes! bulls eye!
0
 
LVL 75

Expert Comment

by:käµfm³d 👽
ID: 36913699
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
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 36915307
Hi there;

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

Kind regards and thanks for your patience.
0
 
LVL 12

Author Comment

by:jazzIIIlove
ID: 37009254
Any updates?

Regards.
0
 
LVL 35

Assisted Solution

by:sarabande
sarabande earned 500 total points
ID: 37102204
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

Featured Post

Upgrade your Question Security!

Your question, your audience. Choose who sees your identity—and your question—with question security.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

High user turnover can cause old/redundant user data to consume valuable space. UserResourceCleanup was developed to address this by automatically deleting user folders when the user account is deleted.
Simulator games are perfect for generating sample realistic data streams, especially for learning data analysis. It is even useful for demoing offerings such as Azure stream analytics, PowerBI etc.
This is Part 3 in a 3-part series on Experts Exchange to discuss error handling in VBA code written for Excel. Part 1 of this series discussed basic error handling code using VBA. http://www.experts-exchange.com/videos/1478/Excel-Error-Handlin…
Despite its rising prevalence in the business world, "the cloud" is still misunderstood. Some companies still believe common misconceptions about lack of security in cloud solutions and many misuses of cloud storage options still occur every day. …

564 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question