c# 创建文件时怎么创建文件夹?大侠急救啊

wing_0706 2012-10-09 10:10:13
strhtml=......
StreamWriter sw=new StreamWriter("D:/test/1.aspx",false);
sw.Write(strhtml);

如上代码,如果test文件夹不存在就会报错,需要先创建test文件夹才会正常产生1.aspx文件,问题:如何动态的自动创建文件夹呢?就是说一个路径,如果有文件夹不存在,就自动创建该文件夹,该如何做?
...全文
3835 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
worldhj1 2012-10-09
  • 打赏
  • 举报
回复
这个上面都回答了
xb12369 2012-10-09
  • 打赏
  • 举报
回复

//以下代码实现了创建文件夹
if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);
}


更多精彩内容,请查看:http://www.cnblogs.com/pegasus923/archive/2011/01/26/1944838.html
全栈极简 2012-10-09
  • 打赏
  • 举报
回复

string directoryPath = @"D:\test";//定义一个路径变量
string filePath = "1.txt";//定义一个文件路径变量
if (!Directory.Exists(directoryPath))//如果路径不存在
{
Directory.CreateDirectory(directoryPath);//创建一个路径的文件夹
}
StreamWriter sw = new StreamWriter(Path.Combine(directoryPath, filePath));
sw.Write("test");
sw.Flush();
sw.Close();
pk3995519 2012-10-09
  • 打赏
  • 举报
回复
/// <summary>
/// 创建文件夹
/// </summary>
/// <param name="FileUrl">路径</param>
public static void CreateFile(string FileUrl)
{
Directory.CreateDirectory(FileUrl);
}
/// <summary>
/// 创建子文件
/// </summary>
/// <param name="FileUrl">路径</param>
/// <param name="matter">内容</param>
public static void CreateTxt(string FileUrl, string matter)
{
//if (!File.Exists(url)) { }
FileStream fs = new FileStream(FileUrl, FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs);
sw.WriteLine(matter);//开始写入值
sw.Close();
fs.Close();
}
wjhgzx 2012-10-09
  • 打赏
  • 举报
回复
public static void Write(string txt,string path,string filename)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
StreamWriter sw = new StreamWriter(Path.Combine(path,filename), false);
sw.Write(txt);
}
wjhgzx 2012-10-09
  • 打赏
  • 举报
回复
public static void Write(string txt,string path,string filename)
{
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
StreamWriter sw = new StreamWriter(path, false);
sw.Write(txt);
}
qldsrx 2012-10-09
  • 打赏
  • 举报
回复
FileInfo fi = new FileInfo("D:/test/1.aspx");
var di = fi.Directory;
if (!di.Exists)
di.Create();
bdmh 2012-10-09
  • 打赏
  • 举报
回复
先分离出文件夹路径,Directory.CreateDirectory创建
adrianEvin 2012-10-09
  • 打赏
  • 举报
回复
Directory.CreateDirectory(filename);
wing_0706 2012-10-09
  • 打赏
  • 举报
回复

太感谢大家了!
lxb768 2012-10-09
  • 打赏
  • 举报
回复
string directoryPath = @"D:\test";//定义一个路径变量
string filePath = "1.txt";//定义一个文件路径变量
if (!Directory.Exists(directoryPath))//如果路径不存在
{
Directory.CreateDirectory(directoryPath);//创建一个路径的文件夹
}
StreamWriter sw = new StreamWriter(Path.Combine(directoryPath, filePath));
sw.Write("test");
sw.Flush();
sw.Close();
超人会飞 2012-10-09
  • 打赏
  • 举报
回复
我跟上面的想法是差不多的
zhoufangpan 2012-10-09
  • 打赏
  • 举报
回复
string path = Server.MapPath("~/UpLoadFiles/MyFile/");


if (!Directory.Exists(path))
{
//创建文件夹

Directory.CreateDirectory(path);
}
enaking 2012-10-09
  • 打赏
  • 举报
回复
string directoryPath = @"D:\test";//定义一个路径变量
string filePath = "1.txt";//定义一个文件路径变量
if (!Directory.Exists(directoryPath))//如果路径不存在
{
Directory.CreateDirectory(directoryPath);//创建一个路径的文件夹
}
StreamWriter sw = new StreamWriter(Path.Combine(directoryPath, filePath));
sw.Write("test");
sw.Flush();
sw.Close();
bigeyescat 2012-10-09
  • 打赏
  • 举报
回复
上面都答完了,反正创建文件时,先用代码判断文件夹存不存在,不存在就先建文件夹,再建文件。

110,535

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • Web++
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧