Developer
Developer

Reputation: 8636

How to create a folder in the application installed directory

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..

Upvotes: 3

Views: 26013

Answers (3)

mint
mint

Reputation: 3433

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

Upvotes: 5

slugster
slugster

Reputation: 49984

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.

Upvotes: 5

g t
g t

Reputation: 7463

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");

Upvotes: 0

Related Questions