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
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# 创建文件时,文件夹不存在,如何自动创建文件夹的更多相关文章

  1. allegro生成光绘文件时,通过cam打开,*.drl钻孔文件不识别,为Unknow类型

    生成钻孔文件时,NC_Parameters中,应该选Absolute

  2. 用file上传文件时,浏览器返回值总是自动添加<pre>的解决办法

    在返回的JSon字符串里面总是莫名其妙的添加了<pre>标签,例如返回内容为"ok",在浏览器里面就变成了"<pre style="word- ...

  3. 013_使用 user.txt 文件中的人员名单,在计算机中自动创建对应的账户并配置初始密码

    for i in `cat user.txt`do    useradd $i    echo "123456" | passwd --stdin $idone

  4. ssh密钥创建分发(端口号非22)&脚本实现自动创建分发密钥

    1.1 服务端端口号变化了,如何基于秘钥连接 1.1.1 环境准备 实验环境: [root@test ~]# cat /etc/redhat-release CentOS release 6.9 (F ...

  5. 外部调用JS文件时出现中文乱码的解决办法

    若测试网页的编码格式为:gb2312,而调用外部JS文件时出现了乱码(前提是JS文件无错误),则将调用的外部JS文件用记事本打开,然后再保存成编码格式为UTF-8的JS文件即可. 若测试网页的编码格式 ...

  6. sublime打开文件时自动生成并打开.dump文件

    GBK Encoding Support 没有安装前打开ASNI格式编码文件会乱码,安装成功重启则可以打开正常 关于.dump文件生成的解释: 当打开一个非utf-8格式且包含汉字的文件时,subli ...

  7. Asp.net 4.0,首次请求目录下的文件时响应很慢

    原文:Asp.net 4.0,首次请求目录下的文件时响应很慢 1. 问题起因2. 尝试过的处理思路3. 解决方法 1. 问题起因 一个从VS2003(.Net Framework 1.1)升级到.ne ...

  8. JS在生成csv文件时,","逗号问题处理.

    在生成csv文件时,发现一个问题,因为csv文件本身是依靠逗号进行分列的,所以内容中有逗号时也被强制分列了,处理方法很简单,为内容加上双引号(英文格式)就可以了. 如: "11111,222 ...

  9. oracle导入dmp文件时出现异常

    oracle导入dmp文件时出现错误 今天在给oracle导入dmp文件时老是出现错误,无论是命令行或PL/SQL,错误截图如下: 经查是导入用户的权限不足,导入用户并没有DBA权限,而导出的dmp文 ...

随机推荐

  1. ASP.NET 验证码 不同浏览器 不刷新问题

    具体为什么不刷新是缓存机制不同,验证码图片的src或ImageUrl的获取是来自一个文件,由于连接地址没变所以不同内核浏览器有的会认为源没有变,解决办法就是在连接后面加上一个随机参数如可以用JS的Ma ...

  2. VS2010中经常使用的快捷键

    1. 格式化对齐:Ctrl+K+F 2. 智能感知:Ctrl+J: 3. 智能感知显示參数信息:Ctrl+Shift+空格: 4. 检查括号匹配(在左右括号间切换): Ctrl +] 5. 选中从光标 ...

  3. LeetCode: Surrounded Regions [130]

    [题目] Given a 2D board containing 'X' and 'O', capture all regions surrounded by 'X'. A region is cap ...

  4. DIV以及图片水平垂直居中兼容多种浏览器

    纯css完美地解决图片以及div垂直水平居中,兼容IE7.0.IE6.0.IE5.5.IE5.0.FF.Opera.Safari具体实现css 如下,感兴趣的朋友可以参考下哈   第一种:全CSS控制 ...

  5. QRMaker生成二维码,支持中文

    QRMaker如果想支持中文,可以将中文转为UTF8,然后用InputDateB直接传入Byte() Option Explicit Private Declare Function WideChar ...

  6. WinRAR 自动解压 解压完成后,执行批处理文件

    部分内容参考网页:http://bbs.kafan.cn/thread-1243208-1-1.html WinRAR 的自动解压文件功能使压缩包也能像 Setup 程序那样,双击后显示一个软件许可, ...

  7. LINUX下查看CPU使用率的命令[Z]

    1.top 使用权限:所有使用者 使用方式:top [-] [d delay] [q] [c] [S] [s] [i] [n] [b] 说明:即时显示process的动态 d :改变显示的更新速度,或 ...

  8. 剑指offer第10题

    import java.util.Scanner; /* 前两种方法是看最低为是不是为1,不为1则向右移动. 第一种只能对正整数有效,对负数不行,因为负数用的是补码,最高外符号位为1,最后右移动,肯定 ...

  9. 关于EJB 时间注解与oracle数据库时间格式

    EJB中Temporal是时间注解,其中TemporalType是时间注解的枚举类型其中包括 TemporalType类型,请看源码/*** Type used to indicate a speci ...

  10. applicationContext.xml 配置文件的存放位置

    eb.xml中classpath:和classpath*:  有什么区别? classpath:只会到你的class路径中查找找文件; classpath*:不仅包含class路径,还包括jar文件中 ...