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 面向 ...
随机推荐
- 在C#中的构造函数和解析函数
构造函数 class A() { A() {Console.write("构造函数");} } 当你在程序种出现 A a=new A();的时候 程序自动执行 构造函数 A() { ...
- 微信小程序开发BUG经验总结
摘要: 常见的微信小程序BUG! 小程序开发越来越热,开发中遇到各种各样的bug,在此总结了一些比较容易掉进去的坑分享给大家. 1. new Date跨平台兼容性问题 在Andriod使用new Da ...
- vue实现双向绑定的简单原理: defineProperty
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Python 3.4:Chromedrive,IEDriverServer,geckoDriver
import sys; import time; import os; #from huoche import PythonTickt; from splinter.browser import Br ...
- √n求单值欧拉函数
基本定理: 首先看一下核心代码: 核心代码 原理解析: 当初我看不懂这段代码,主要有这么几个问题: 1.定理里面不是一开始写了一个n*xxx么?为什么代码里没有*n? 2.ans不是*(prime[i ...
- SD从零开始67-70 后勤信息系统中的标准分析, 信息结构, 信息的更新规则, 建立统计数据
SD从零开始67 后勤信息系统中的标准分析 标准分析中的报表Reporting in Standard Analyses 标准分析为高质量的表达和分析LIS中的数据基础提供了大量的功能: 当你决定了一 ...
- Units in Android
一般使用dp,不使用px.sp啥时候用呢?给TextView设置文字大小的时候用.
- (网页)JS和CSS不缓存方法,时间戳
<link ..... href=".....css?time"+new Date()> <script type="text/javascript&q ...
- mysql索引类型 normal, unique, full text
问题1:mysql索引类型normal,unique,full text的区别是什么? normal:表示普通索引 unique:表示唯一的,不允许重复的索引,如果该字段信息保证不会重复例如身份证号用 ...
- Django中ORM介绍和字段及字段参数 Object Relational Mapping(ORM)
Django中ORM介绍和字段及字段参数 Object Relational Mapping(ORM) ORM介绍 ORM概念 对象关系映射(Object Relational Mapping,简 ...