views:

56

answers:

3

I have done an application in c# windows application. In this i created a project with name ACHWINAPP. I have written some code to get the path that i required as follows

strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH\\";

But when i create a setup for the project and installed in a direcotry namely some F:\ i am getting the error ACH as not found .

What i need is when user clicks on save i would like to save the file in the directory where he installed my setup file with the folder name ACH Any Idea please..

A: 

It's hard to understand exactly what you want. Maybe use the answers to this question to load files next to the currently running application?

Otherwise, either trace out using Console.WriteLine() (or if you're using Visual Studio, add a Breakpoint) to find out the initial value of strFilePath. It's probably not what you expect.

Rather than 'adding' strings together, use Path.Combine(path1, path2) to create your path:

strFilePath = Path.Combine(strFilePath, "ACH");
gt
Hey what i need is i have created my application in my system in the dirctory E with a name ACHWINAPP. On my PC it works fine. If i create a setup for what i did and if i installed it in on some other PC in some directory C:\ i am unable to find the folder name ACH so that i am unable to save the files for the user in the installed direcotry
Dorababu
+1  A: 

This is a relatively simple bit of code:

string currentPath = Directory.GetCurrentDirectory();
if (!Directory.Exists(Path.Combine(currentPath, "ACH")))
    Directory.CreateDirectory(Path.Combine(currentPath, "ACH"));
//at this point your folder should exist

of course there can be a bunch of reasons why you can fail to create the folder, including insufficient privileges to do so. So you should also practice safe coding and catch exceptions when dealing with the file system.

slugster
This was Helpful but as my installed path is this C:\Program Files\Default Company Name\ACH i would like to create that folder in this path but it is getting create in C:\Program Files
Dorababu
+1  A: 

Do you mean:

Application.StartupPath

Might not be what you want... but its the folder from which your executable is located

Link: http://msdn.microsoft.com/en-us/library/system.windows.forms.application.startuppath.aspx

mint