C# 如何在目录中创建子目录
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/771564/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to create a sub directory inside a directory
提问by
In other words, i have temp folder where i store my extracted files. How do i create a folder in that temp folder so that all files are extracted or unzipped in this folder, which is inside the temp folder?
换句话说,我有临时文件夹,用于存储我提取的文件。我如何在该临时文件夹中创建一个文件夹,以便在该文件夹中提取或解压缩所有文件,该文件夹位于临时文件夹内?
回答by Kirtan
Simple
简单的
Directory.CreateDirectory(Path.Combine("<Your temp folder>", "<DirectoryName>"));
Make sure you have the proper rights given to the aspnet worker process to create the folder.
确保您有适当的权限授予 aspnet 工作进程以创建文件夹。
回答by snowcrash09
System.IO.Directory.CreateDirectory()
is what you're looking for.
回答by Leonid Shirmanov
string tempFolderAbsolutePath = @"C:\Temp";
string subFolderRelativePath = @"SubTemp1";
DirectoryInfo tempFolder = new DirectoryInfo( tempFolderAbsolutePath );
DirectoryInfo subFolder = tempFolder.CreateSubdirectory( subFolderRelativePath );
string tempFileName = String.Concat( Guid.NewGuid().ToString(), @".tmp" );
string textData = @"Temp text data";
using (StreamWriter streamWriter = File.CreateText( Path.Combine( subFolder.FullName, tempFileName ) ))
{
streamWriter.Write( textData );
streamWriter.Flush();
streamWriter.Close();
}
回答by Hadi Barak
Simply Use This :
只需使用这个:
System.IO.Directory.CreateDirectory(String.Format(@"{0}/{1}", PathToParent, SubDirectoryName)
回答by Rakesh Dadamatti
FRONT END
前端
<form id="form1" runat="server">
<asp:Button ID="Button1" runat="server" Text="Create New Directory" onclick="createButton_Click" />
<br /><br />
<asp:Label ID="Label1" runat="server" Text=""></asp:Label><br />
<asp:Label ID="Label2" runat="server" Text=""></asp:Label>
</form>
CODEBEHIND
代码隐藏
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Configuration;
using System.IO;
namespace RakeshDadamatti
{
public partial class CreateDirectory : System.Web.UI.Page
{
String newDirectory;
String subDirectory;
protected void Page_Load(object sender, EventArgs e)
{
}
private void CreatenewDirectory(string newDirectory)
{
try
{
if (!Directory.Exists(newDirectory))
{
Directory.CreateDirectory(newDirectory);
Label1.Text = "Directory Has Been Created.";
}
else
{
Label1.Text = "Directory Exists.";
}
if (!Directory.Exists(subDirectory))
{
Directory.CreateDirectory(subDirectory);
Label2.Text = "Sub Directory Has Been Created.";
}
else
{
Label2.Text = "Sub Directory Exists.";
}
}
catch (IOException _err)
{
Response.Write(_err.Message);
}
}
protected void createButton_Click(object sender, EventArgs e)
{
newDirectory = Server.MapPath("Directory Name Here");
subDirectory = Server.MapPath(@"" + "~/" + newDirectory + "/" + "Sub Directory Name Here");
CreatenewDirectory(newDirectory);
}
}
}