How to create a folder in the application installed directory

心不动则不痛 提交于 2019-12-10 23:58:17

问题


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


来源:https://stackoverflow.com/questions/3343206/how-to-create-a-folder-in-the-application-installed-directory

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!