Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在应用程序安装目录中创建文件夹_C#_Desktop Application - Fatal编程技术网

C# 如何在应用程序安装目录中创建文件夹

C# 如何在应用程序安装目录中创建文件夹,c#,desktop-application,C#,Desktop Application,我已经在c#windows应用程序中完成了一个应用程序。在本文中,我创建了一个名为ACHWINAPP的项目。我已经编写了一些代码来获得我需要的路径,如下所示 strFilePath = Directory.GetCurrentDirectory(); strFilePath = Directory.GetParent(strFilePath).ToString(); strFilePath = Directory.GetParent(strFilePath).ToString(); strFil

我已经在c#windows应用程序中完成了一个应用程序。在本文中,我创建了一个名为ACHWINAPP的项目。我已经编写了一些代码来获得我需要的路径,如下所示

strFilePath = Directory.GetCurrentDirectory();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = Directory.GetParent(strFilePath).ToString();
strFilePath = strFilePath + "\\ACH\\";
但是,当我为项目创建一个安装程序并安装在一个direcotry中,即一些F:\时,我得到的错误是未找到ACH

我需要的是,当用户单击“保存”时,我希望将文件保存在他安装我的安装文件的目录中,文件夹名为ACH
有什么想法吗?

很难准确理解你想要什么。是否可以使用答案加载当前运行的应用程序旁边的文件

否则,请使用
Console.WriteLine()
(或者如果您使用的是Visual Studio,请添加断点)进行跟踪,以找出strFilePath的初始值。这可能不是你所期望的

使用
Path.Combine(路径1,路径2)
创建路径,而不是将字符串“添加”在一起:

strFilePath = Path.Combine(strFilePath, "ACH");

这是一段相对简单的代码:

string currentPath = Directory.GetCurrentDirectory();
if (!Directory.Exists(Path.Combine(currentPath, "ACH")))
    Directory.CreateDirectory(Path.Combine(currentPath, "ACH"));
//at this point your folder should exist
当然,创建文件夹失败可能有很多原因,包括权限不足。因此,在处理文件系统时,您还应该练习安全编码并捕获异常。

您的意思是:

Application.StartupPath
可能不是你想要的。。。但它是可执行文件所在的文件夹


链接:

嘿,我需要的是我已经在目录E中的系统中创建了名为ACHWINAPP的应用程序。在我的电脑上,它工作正常。如果我为我所做的创建了一个安装程序,并将其安装在其他PC上的某个目录C:\i无法找到文件夹名称ACH,因此无法将用户的文件保存在已安装的目录中,这很有帮助,但由于我的安装路径是C:\Program files\Default Company name\ACH,我希望在其中创建该文件夹此路径无效,但正在C:\Program文件中创建