用C#创建目录 [英] Create a Directory in C#

查看:65
本文介绍了用C#创建目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述





我在C#中创建目录

Hi

I am Creating a directory in C#

if (rows_affected == true)
        {

        
           Directory.CreateDirectory(Server.MapPath(@"~/Documents/Category/")+ Category_Id);
           
        }



此代码使用Category_Id创建目录



现在我想要要创建一个子目录,我想在类别ID文件夹中创建一个目录我是如何做到这一点的 Category_Id 是不固定的。




This Code Creating a directory with Category_Id

Now I want to Create a SubDirectory , I want to create a directory in Category Id Folder How I do this and Category_Id is not fixed.

if (rows_affected == true)
        {
            Directory.CreateDirectory(Server.MapPath(@"~/Documents/Category") + category_id +  subcategory_id);
          
            Response.Redirect("~/List_SubCategory.aspx");
            Message_Label.ForeColor = System.Drawing.Color.Green;
        }

推荐答案

基本上,你需要在Category_Id和subcategory_id
目前,(假设您的Cat ID为ABC且您的子猫为DEF),您要求系统创建

Basically, you need a "\" between the "Category_Id" and the "subcategory_id"
At the moment, (assuming your Cat ID is "ABC" and your sub cat is "DEF") you are asking teh system to create
D:\Temp\ABC

然后

And then

D:\Temp\ABCDEF

何时你想要的是

When what you want is

D:\Temp\ABC\DEF



最好的方法这是使用Path.Combine:


The best way to do this is to use Path.Combine:

string path = Path.Combine(Server.MapPath(@"~/Documents/Category/"), category_id,  subcategory_id);
Directory.CreateDirectory(path)

因为这会根据需要添加和删除斜杠。

As this will add and remove slashes as necessary.


这篇关于用C#创建目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆