C# 创建文件时,文件夹不存在,如何自动创建文件夹
2013-12-27 14:08
751 查看
c# 创建文件时怎么创建文件夹?
strhtml=......
StreamWriter sw=new StreamWriter("D:/test/1.aspx",false);
sw.Write(strhtml);
如上代码,如果test文件夹不存在就会报错,需要先创建test文件夹才会正常产生1.aspx文件,问题:如何动态的自动创建文件夹呢?就是说一个路径,如果有文件夹不存在,就自动创建该文件夹,该如何做?
------解决方案--------------------
Directory.CreateDirectory(filename);
------解决方案--------------------
先分离出文件夹路径,Directory.CreateDirectory创建
------解决方案--------------------
C# code
C# code
C# code
strhtml=......
StreamWriter sw=new StreamWriter("D:/test/1.aspx",false);
sw.Write(strhtml);
如上代码,如果test文件夹不存在就会报错,需要先创建test文件夹才会正常产生1.aspx文件,问题:如何动态的自动创建文件夹呢?就是说一个路径,如果有文件夹不存在,就自动创建该文件夹,该如何做?
------解决方案--------------------
Directory.CreateDirectory(filename);
------解决方案--------------------
先分离出文件夹路径,Directory.CreateDirectory创建
------解决方案--------------------
C# code
FileInfo fi = new FileInfo("D:/test/1.aspx"); var di = fi.Directory; if (!di.Exists) di.Create(); ------解决方案-------------------- 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); } ------解决方案-------------------- /// <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(); } ------解决方案--------------------
C# code
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(); ------解决方案--------------------
C# code
//以下代码实现了创建文件夹
if (!Directory.Exists(sPath))
{
Directory.CreateDirectory(sPath);
}
------解决方案--------------------
这个上面都回答了
------解决方案--------------------
上面都答完了,反正创建文件时,先用代码判断文件夹存不存在,不存在就先建文件夹,再建文件。
------解决方案--------------------
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(); ------解决方案--------------------
string path = Server.MapPath("~/UpLoadFiles/MyFile/");
if (!Directory.Exists(path))
{
//创建文件夹
Directory.CreateDirectory(path);
}
------解决方案--------------------
我跟上面的想法是差不多的
------解决方案--------------------
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();
相关文章推荐
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库
- Java 在给定路径上创建文件,所在文件夹不存在时,如何正确创建
- Java 在给定路径上创建文件,所在文件夹不存在时,如何正确创建。
- C#判断文件和文件夹是否存在 不存在则创建
- 模拟dos中的xcopy功能,实现文件的复制,当子文件夹不存在时自动创建
- 如何在C#中用程序执行指定的SQL脚本文件,实现自动安装创建数据库.txt
- Java如何在不存在文件夹的目录下创建文件
- Java如何判断文件或者文件夹是否在?不存在如何创建?
- Java 在给定路径上创建文件,所在文件夹不存在时,如何正确创建。
- ASP如何检测某文件夹是否存在,不存在则自动创建
- Java 在给定路径上创建文件,所在文件夹不存在时,如何正确创建。
- Java如何判断文件或者文件夹是否在?不存在如何创建?
- php判断文件夹或文件是否存在,及不存在时如何创建
- C# 如何获取某文件夹的文件列表,并按文件的创建时间排序?
- 【C#】C#判断文件路径是否存在,不存在则创建文件夹
- C#判断文件和文件夹是否存在 不存在则创建
- 用java实现自动创建文件及文件夹
- C#.net获取指定文件夹下的所有jpg文件,并按文件创建时间排序
- 如何自动为C#程序文件添加版权信息注释
- C#中动态创建文件夹及文件