How to create a folder in the application installe

2019-07-19 02:23发布

问题:

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

回答1:

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



回答2:

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.



回答3:

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