C# 下载文件 删除文件 写入文本
由于经常用到文件处理,便自己封装了下 分享给大家。 包含写入文本 批量删除文件 下载文件 。--可直接使用
/// <summary>
/// 写入到txt
/// </summary>
/// <param name="savePath"></param>
/// <param name="content"></param>
public static void WriteInTxt(string savePath, string content)
{
string tempPath = System.IO.Path.GetDirectoryName(savePath);
System.IO.Directory.CreateDirectory(tempPath); //创建临时文件目录
if (!System.IO.File.Exists(savePath))
{
FileStream fs1 = new FileStream(savePath, FileMode.Create, FileAccess.Write);//创建写入文件
StreamWriter sw = new StreamWriter(fs1);
sw.WriteLine(content);//开始写入值
sw.Close();
fs1.Close();
}
else
{
FileStream fs = new FileStream(savePath, FileMode.Open, FileAccess.Write);
StreamWriter sr = new StreamWriter(fs);
sr.WriteLine(content);//开始写入值
sr.Close();
fs.Close();
}
}
/// <summary>
/// 递归删除文件夹下所有文件
/// </summary>
/// <param name="file"></param>
public static void DeleteFile(string dirPath)
{
try
{
//去除文件夹和子文件的只读属性
//去除文件夹的只读属性
System.IO.DirectoryInfo fileInfo = new DirectoryInfo(dirPath);
fileInfo.Attributes = FileAttributes.Normal & FileAttributes.Directory;
//去除文件的只读属性
System.IO.File.SetAttributes(dirPath, System.IO.FileAttributes.Normal);
//判断文件夹是否还存在
if (Directory.Exists(dirPath))
{
foreach (string f in Directory.GetFileSystemEntries(dirPath))
{
if (File.Exists(f))
{
//如果有子文件删除文件
File.Delete(f);
}
else
{
//循环递归删除子文件夹
DeleteFile(f);
}
}
//删除空文件夹
Directory.Delete(dirPath);
}
}
catch (Exception e)
{
}
}
/// <summary>
/// Http下载文件
/// </summary>
/// <param name="url">下载文件路径</param>
/// <param name="savePath">保存路径</param>
/// <returns></returns>
public static bool HttpDownloadFile(string url, string savePath)
{
string tempPath = System.IO.Path.GetDirectoryName(savePath);
System.IO.Directory.CreateDirectory(tempPath); //创建临时文件目录
string tempFile = tempPath + @"\" + System.IO.Path.GetFileName(savePath); //临时文件
if (System.IO.File.Exists(tempFile))
{
//存在则跳出
return true;
//System.IO.File.Delete(tempFile);
}
try
{
FileStream fs = new FileStream(tempFile, FileMode.Append, FileAccess.Write, FileShare.ReadWrite);
// 设置参数
HttpWebRequest request = WebRequest.Create(url) as HttpWebRequest;
//发送请求并获取相应回应数据
HttpWebResponse response = request.GetResponse() as HttpWebResponse;
//直到request.GetResponse()程序才开始向目标网页发送Post请求
Stream responseStream = response.GetResponseStream();
//创建本地文件写入流
//Stream stream = new FileStream(tempFile, FileMode.Create);
byte[] bArr = new byte[1024];
int size = responseStream.Read(bArr, 0, (int)bArr.Length);
while (size > 0)
{
//stream.Write(bArr, 0, size);
fs.Write(bArr, 0, size);
size = responseStream.Read(bArr, 0, (int)bArr.Length);
}
//stream.Close();
fs.Close();
responseStream.Close();
System.IO.File.Move(tempFile, savePath);
return true;
}
catch (Exception ex)
{
return false;
}
}
C# 下载文件 删除文件 写入文本的更多相关文章
- python:创建文件夹:写入文本1:读取txt:读取Excel文件遍历文件夹:
https://blog.csdn.net/u011956147/article/details/80369731 创建文件夹: import osimport shutil def buildfil ...
- SVN的搭建及使用(三)用TortoiseSVN修改文件,添加文件,删除文件,以及如何解决冲突,重新设置用户名和密码等
添加文件 在检出的工作副本中添加一个Readme.txt文本文件,这时候这个文本文件会显示为没有版本控制的状态,如图: 这时候,你需要告知TortoiseSVN你的操作,如图: 加入以后,你的文件会变 ...
- php读取zip文件(删除文件,提取文件,增加文件)实例
<?php /* php 从zip压缩文件中提取文件 */ $zip = new ZipArchive; if ($zip->open('jQuery五屏上下滚动焦点图代码.zip') = ...
- C#递归拷贝文件删除文件
拷贝文件及子文件,最后一个参数排除,哪个不要删除.(其实就是移动的效果) //拷贝文件及子文件 public static void CopyDirectory(string src, string ...
- Java——如何创建文件夹及文件,删除文件,文件夹
package com.zz; import java.io.File; import java.io.IOException; /** * Java创建文件夹 */ public class Cre ...
- WCF上传、下载、删除文件
关键代码: --上传的stream处理,转为bytep[] private void Parse(Stream stream, Encoding encoding) { this.Success = ...
- Open XML的上传、下载 、删除 ......文件路径
/// <summary> /// Get download site, if download tempfolder not existed, create it first /// & ...
- Python习题-列出目录下所有文件删除文件夹
需求描述: 1.当前目录下有很多文件夹.文件,统计/usr/local/这个目录下,如果是文件夹,就给删除 /usr/local/ f1 w1 f2 w2 w3 w4 ...
- python下载网页源码 写入文本
import urllib.request,io,os,sysreq=urllib.request.Request("http://echophp.sinaapp.com/uncategor ...
随机推荐
- struts框架问题四之获取到值栈的对象
4. 问题四 : 如何获得值栈对象 * 获得值栈对象 有三种方法 * ValueStack vs1 = (ValueStack) ServletActionContext.getRequest().g ...
- On Line Tools
1)在线生成证书请求 https://www.icewarp.com/support/online_help/3206.htm 2)在线文件共享 https://reep.io/ https://sn ...
- 团队项目:二次开发--v.2.1--软件工程
原先代码,对于基本对象的Get,Set方法构造函数等方法与实现基本功能的方法统一放到了一起,容易造成代码不清晰,别人比较难阅读的情况.而且其中代码冗余比较多. 改进代码,进行了层次的分析,将基本对象与 ...
- pip安装python模块方法
网上搜索了很多,主流的配置方法分为两种: 摘自 1.http://www.jb51.net/article/83617.htm 安装pip的包并确定pip安装时的镜像源地址,国内常用的地址有: htt ...
- 1.spring环境的搭建
1.app.config <?xml version="1.0" encoding="utf-8" ?><configuration> ...
- 测试这个才可以打包 我的PYQt matplotlib numpy 等程序
from distutils.core import setup import py2exe import matplotlib import sys import FileDialog import ...
- VS快捷键以及Reshaper快捷键
VS快捷键: Resharper 快捷键(此图是保存他人的[具体是谁忘记了]): 参考: http://msdn.microsoft.com/zh-cn/library/da5kh0wa.aspx
- KbmMW 认证管理器说明(转载)
这是kbmmw 作者关于认证管理器的说明,我懒得翻译了,自己看吧. There are 5 parts of setting up an authorization manager: A) Defin ...
- 24. Indoor Air pollution 室内空气污染
. Indoor Air pollution 室内空气污染 ① Priscilla Ouchida's "energy-efficient"house turned out to ...
- java最全的Connection is read-only. Queries leading to data modification are not allowed
Connection is read-only. Queries leading to data modification are not allowed 描述:框架注入时候,配置了事物管理,权限设置 ...