在c#项目中动态创建文件夹

本文关键字:创建 文件夹 动态 项目 | 更新日期: 2023-09-27 17:50:41

我必须动态地在C#项目中创建一个文件夹并向其中添加文件。

尝试了下面的代码,但是它在bin文件夹中创建了文件夹。我需要在项目运行路径本身创建。

//代码:

// Specify a name for your top-level folder. 
string folderName = @"..'Top-Level Folder";
System.IO.Directory.CreateDirectory(folderName);

我错在哪里?

在c#项目中动态创建文件夹

我猜你在找这个

 string projectPath = Directory.GetParent(Directory.GetCurrentDirectory()).Parent.FullName;
 string folderName = Path.Combine(projectPath, "Top-Level Folder");
 System.IO.Directory.CreateDirectory(folderName);

不确定你想要实现什么,但你可以使用项目属性来创建预/后构建操作。

bin文件夹下创建的原因是代码是从bin/debug

运行的。

如果你能说出你想要做什么,也许有更好的解决方案