如何在控制台应用程序中创建文件夹和子文件夹

如何在控制台应用程序中创建文件夹和子文件夹

本文介绍了如何在控制台应用程序中创建文件夹和子文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在控制台应用中创建几个文件夹。一些文件夹有两个或三个子文件夹,有些有一个子文件夹,这里是文件夹结构;



I need to create a couple of folders in a console app. some folders have two or three subfolders, and some have one subfolder, here is the folder structure;

Survey
    Photos
    Forms
    Report
Install
   Photos
Design
    Initial drawing
Inspection
Permit
    Approval permit
Proposal





我不想在应用程序中硬编码,任何有关如何处理它的建议。谢谢



I don't want to hardcoded in app, any suggestion on how I can handle it nicely. Thanks

推荐答案

引用:

我不知道我想在应用程序中硬编码,

I don't want to hardcoded in app,





这是否意味着你要排除





Does that mean you are ruling out

Directory.CreateDirectory(@"Survey\Photos");
Directory.CreateDirectory(@"Survey\Forms");
Directory.CreateDirectory(@"Survey\Report");
Directory.CreateDirectory(@"Install\Photos");







??



您当然可以将路径存储为db,JSON,xml等中的字符串并循环遍历它们,但这仍然是硬编码



一种替代方案是

1)在磁盘上创建目录结构

2)压缩目录结构

3)在您的应用程序中包含压缩目录结构作为资源

4)当程序运行时,它可以检查目录结构是否存在,如果不存在,则获取资源zip并解压缩到你需要它的'root'




??

you could of course store the paths as strings in a db, JSON, xml etc and loop through them, but that's still 'hardcoding'

One alternative would be
1) create the directory structure on disk
2) zip up the directory structure
3) include the zipped directory structure as a resource in your application
4) when the program runs, it could check if the directory structures exists, if not, get the resource zip and unzip it to the 'root' where you need it


if (!IO.Directory.Exists(@"C:\Survey\Photos")
{
  IO.Directory.CreateDirectory(@"C:\Survey\Photos")
}


这篇关于如何在控制台应用程序中创建文件夹和子文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-12 22:32