C# 创建文件时,文件夹不存在,如何自动创建文件夹
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# 创建文件时,文件夹不存在,如何自动创建文件夹的更多相关文章
- allegro生成光绘文件时,通过cam打开,*.drl钻孔文件不识别,为Unknow类型
生成钻孔文件时,NC_Parameters中,应该选Absolute
- 用file上传文件时,浏览器返回值总是自动添加<pre>的解决办法
在返回的JSon字符串里面总是莫名其妙的添加了<pre>标签,例如返回内容为"ok",在浏览器里面就变成了"<pre style="word- ...
- 013_使用 user.txt 文件中的人员名单,在计算机中自动创建对应的账户并配置初始密码
for i in `cat user.txt`do useradd $i echo "123456" | passwd --stdin $idone
- ssh密钥创建分发(端口号非22)&脚本实现自动创建分发密钥
1.1 服务端端口号变化了,如何基于秘钥连接 1.1.1 环境准备 实验环境: [root@test ~]# cat /etc/redhat-release CentOS release 6.9 (F ...
- 外部调用JS文件时出现中文乱码的解决办法
若测试网页的编码格式为:gb2312,而调用外部JS文件时出现了乱码(前提是JS文件无错误),则将调用的外部JS文件用记事本打开,然后再保存成编码格式为UTF-8的JS文件即可. 若测试网页的编码格式 ...
- sublime打开文件时自动生成并打开.dump文件
GBK Encoding Support 没有安装前打开ASNI格式编码文件会乱码,安装成功重启则可以打开正常 关于.dump文件生成的解释: 当打开一个非utf-8格式且包含汉字的文件时,subli ...
- Asp.net 4.0,首次请求目录下的文件时响应很慢
原文:Asp.net 4.0,首次请求目录下的文件时响应很慢 1. 问题起因2. 尝试过的处理思路3. 解决方法 1. 问题起因 一个从VS2003(.Net Framework 1.1)升级到.ne ...
- JS在生成csv文件时,","逗号问题处理.
在生成csv文件时,发现一个问题,因为csv文件本身是依靠逗号进行分列的,所以内容中有逗号时也被强制分列了,处理方法很简单,为内容加上双引号(英文格式)就可以了. 如: "11111,222 ...
- oracle导入dmp文件时出现异常
oracle导入dmp文件时出现错误 今天在给oracle导入dmp文件时老是出现错误,无论是命令行或PL/SQL,错误截图如下: 经查是导入用户的权限不足,导入用户并没有DBA权限,而导出的dmp文 ...
随机推荐
- Productivity Improvements for the Entity Framework(实体框架设计)【转】
Background We’ve been hearing a lot of good feedback on the recently released update to the Entity F ...
- hdu 1011 Starship Troopers (树形背包dp)
本文出自 http://blog.csdn.net/shuangde800 题目链接 : hdu-1011 题意 有n个洞穴编号为1-n,洞穴间有通道,形成了一个n-1条边的树, 洞穴的入口即 ...
- C++游戏编程(一开篇)
本系列文章由zhmxy555(毛星云)编写,转载请注明出处. http://blog.csdn.net/zhmxy555/article/details/7318264 作者:毛星云 邮箱: h ...
- asp-net-web-api 自定义URl插件
http://attributerouting.net/#asp-net-web-api https://github.com/johnpapa/CodeCamper/blob/master/Code ...
- FPGA开发(3)
转载 榨干FPGA片上存储资源 记得Long long time ago,特权同学写过一篇简短的博文<M4K使用率>,文章中提到了Cyclone器件的内嵌存储块M4K的配置问题.文中提到了 ...
- Php5.3的lambda函数以及closure(闭包)
从php5.3以后,php也可以使用lambda function(可能你会觉得是匿名函数,的确是但不仅仅是)来写类似javascript风格的代码: $myFunc = function() { e ...
- Android minHeight/Width,maxHeight/Width
在layout文件中,设置IamgeView的最大(最小)高度(宽度)时,需要同时设置android:adjustViewBounds="true",这样设置才会生效.在代码中设置 ...
- Android Material Design调色板
转: http://www.stormzhang.com/design/2014/12/26/material-design-palette/ Material Design出来一段时间了,身为And ...
- CentOS下Mysql安装调试
一.安装 yum安装:yum install -y mysql-server mysql mysql-devel 设置自启动:chkconfig mysqld on 启动MySQL:service ...
- swift 创建tableView并实现协议
// // ViewController2.swift // swift_helloword // // Created by Charlie on 15/7/13. // Copyright (c) ...