ASP.NET 文件操作类
1.读取文件
2.写入文件
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml; namespace BigData.Common.File
{
/// <summary>
/// 文件操作类
/// </summary>
public class FileHelper
{
/// <summary>
/// 写入一行数据
/// </summary>
/// <param name="path"></param>
/// <param name="str"></param>
/// <param name="isAppend"></param>
/// <returns></returns>
public static bool WriteLine(string path, string str, bool isAppend = true)
{
try
{
StreamWriter sw = new StreamWriter(path, isAppend);
sw.WriteLine(str);
sw.Flush();
sw.Close();
}
catch (Exception)
{
return false;
} return true;
} /// <summary>
/// 读取文件所有内容
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string ReadAll(string path)
{
string result = string.Empty;
try
{
StreamReader sr = new StreamReader(path);
result = sr.ReadToEnd();
sr.Close();
}
catch (Exception)
{
return string.Empty;
} return result;
} /// <summary>
/// 写入文本
/// </summary>
/// <param name="path"></param>
/// <param name="str"></param>
/// <returns></returns>
public static bool WriteToTxt(string path, string str)
{
try
{
FileStream fs = new FileStream(path, FileMode.Append);
byte[] bs = Encoding.Default.GetBytes(str);
fs.Write(bs, , bs.Length);
fs.Flush();
fs.Close();
}
catch (Exception)
{
return false;
} return true;
} /// <summary>
/// 读取文本
/// </summary>
/// <param name="path"></param>
/// <returns></returns>
public static string ReadTxt(string path)
{
string result = string.Empty;
try
{
FileStream fs = new FileStream(path, FileMode.OpenOrCreate);
byte[] bs = new byte[fs.Length];
fs.Read(bs, , bs.Length);
result = Encoding.Default.GetString(bs);
fs.Close();
}
catch (Exception)
{
return string.Empty;
} return result;
}
}
}
ASP.NET 文件操作类的更多相关文章
- asp.net文件操作类
/** 文件操作类 **/ #region 引用命名空间 using System; using System.Collections.Generic; using System.Text; usin ...
- [C#] 常用工具类——文件操作类
/// <para> FilesUpload:工具方法:ASP.NET上传文件的方法</para> /// <para> FileExists:返回文件是否存在&l ...
- 文件操作类CFile
CFile file; CString str1= L"写入文件成功!"; wchar_t *str2; if (!file.Open(L"Hello.txt" ...
- android 文件操作类简易总结
android 文件操作类(参考链接) http://www.cnblogs.com/menlsh/archive/2013/04/02/2997084.html package com.androi ...
- Ini文件操作类
/// <summary> /// Ini文件操作类 /// </summary> public class Ini { // 声明INI文件的写操作函数 WritePriva ...
- java csv 文件 操作类
一个CSV文件操作类,功能比较齐全: package tool; import java.io.BufferedReader; import java.io.BufferedWriter; impor ...
- Qt5:Qt文件操作类 QFile
在QT中,操作文件一般不使用C++提供的文件操作类 , 因为操作文件的时候,要用到C++提供的 string 类,而在QT中使用的是Qt自己实现的一个string类 QString .在Qt中使用C+ ...
- C# 文件操作类大全
C# 文件操作类大全 时间:2015-01-31 16:04:20 阅读:1724 评论:0 收藏:0 [点我收藏+] 标签: 1.创建文件夹 //usin ...
- Java文件操作类效率对比
前言 众所周知,Java中有多种针对文件的操作类,以面向字节流和字符流可分为两大类,这里以写入为例: 面向字节流的:FileOutputStream 和 BufferedOutputStream 面向 ...
随机推荐
- Spring基于XML方式的使用
一.IoC配置 IoC的配置是通过Spring的xml文件的bean标签进行的. 1.bean标签介绍 bean标签一般是在xml文件进行配置的,xml文件一般样式如下: <?xml versi ...
- 9种网页Flash焦点图和jQuery焦点图幻灯片
jQuery图标放大轮播焦点图 Flash图片焦点图滑动切换 Flash右侧焦点图上下滑动切换 左右按钮滑动切换的网页幻灯片 双图同时滑动切换的焦点图 含有上下按钮的双图同时滑动切换的焦点图 常见的j ...
- canvas-2lineCap.html
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- 【工具相关】Web-ionic-npm的安装
一, 一般最新版本的Node.js集悲剧了npm.在命令行下,输入命令: "npm-v",可以测试npm是否安装.如下图所示: 二,上图显示了npm的版本号,即代表已经安装了npm ...
- maven 技巧
M2Eclipse Releases maven eclipse插件最新安装地址 Full Version Tag 1.0 2011-06-22 http://download.eclipse.org ...
- ionic cordova 安装指定版本
安装ionic 及 cordova npm install -g cordova ionic npm 淘宝镜像(GFW,导致很多插件下载失败) npm install -g cnpm --regist ...
- loadrunner 运行脚本-Run-time Settings-ContentCheck简单设置
运行脚本-Run-time Settings-ContentCheck简单设置 by:授客 QQ:1033553122 ContentCheck的设置可用来让VuGen检测存在错误的站点页面.如果被测 ...
- Android中的padding和margin的区别
在Android的布局中,常常有人将padding和margin搞混,他们其实不一样的,padding是该控件的内部距离. magin是该控件与其他控件之间的距离.例如 <LinearLayou ...
- 四则运算 Java 姚康友,黎扬乐
github项目传送门:https://github.com/yaokangyou/arithmetic 项目要求 功能列表 [完成] 使用 -n 参数控制生成题目的个数 [完成] 使用 -r 参数控 ...
- 使用openssl在windows 10下本地xampp配置https开发环境
安装win64OpenSSL-1_1_0j后重新启动:以管理员权限启动powershell; 执行以下命令 set OPENSSL_CONF=c:\xampp\apache\conf\openssl. ...