c#对文件流的操作
来源:互联网 发布:淘宝购买万艾可的流程 编辑:程序博客网 时间:2024/06/09 19:10
//写文件
private static bool WriteFile(byte[] byData, string path)
{
try
{
string filepath = System.Web.HttpContext.Current.Server.MapPath(path);
if (File.Exists(path))
File.Delete(path);
System.IO.FileStream fs = System.IO.File.Create(path);
fs.Write(byData, 0, byData.Length);
fs.Flush();
fs.Close();
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
}
}
//生成一个文件
public static bool CreateFile(string filecontent, string path)
{
try
{
string filePath = System.Web.HttpContext.Current.Server.MapPath(path);
if (File.Exists(filePath))
File.Delete(filePath);
else
{
System.IO.FileStream fs = System.IO.File.Create(path);
byte[] byContent = System.Text.Encoding.GetEncoding("gb2312").GetBytes(filecontent);
fs.Write(byContent, 0, byContent.Length);
fs.Flush();
fs.Close();
}
return true;
}
catch (Exception ex)
{
return false;
}
finally
{
}
}
//读入文件
public static string WriteFile(string path)
{
try
{
string filepath = System.Web.HttpContext.Current.Server.MapPath(path);
if (File.Exists(filepath))
return "false";
else
{
string fileread = System.IO.File.ReadAllText(filepath, System.Text.Encoding.GetEncoding("gb2312"));
return fileread;
}
}
catch (Exception ex)
{
return "false";
}
finally
{
}
}
//向文件中增加内容
public static bool UpdateFile(string filecontent, string path)
{
try
{
string filePath = System.Web.HttpContext.Current.Server.MapPath(path);
if (!File.Exists(filePath))
{
System.IO.FileStream fs = System.IO.File.Create(filePath);
byte[] byContent = System.Text.Encoding.GetEncoding("gb2312").GetBytes(filecontent);
fs.Write(byContent, 0, byContent.Length);
fs.Flush();
fs.Close();
}
System.IO.FileStream files = new System.IO.FileStream(filePath, FileMode.Open, FileAccess.ReadWrite);
StreamReader sReader = new StreamReader(files, System.Text.Encoding.Default);
string oldstr = sReader.ReadToEnd();
oldstr += filecontent;
//把新的内容重新写入
StreamWriter sWriter = new StreamWriter(files, System.Text.Encoding.Default);
sWriter.Write(oldstr);
sWriter.Flush();
sWriter.Close();
sReader.Close();
files.Close();
return true;
}
catch (Exception ex)
{
return false;
}
}
//删除文件
public static bool Delete(string path)
{
try
{
string filePath = System.Web.HttpContext.Current.Server.MapPath(path);
if (File.Exists(path))
File.Delete(filePath);
return true;
}
catch (Exception ex)
{
return false;
}
finally { }
}
//创建文件夹
public static bool CreateFolder(string FolderName)
{
try
{
if (FolderName.Trim().Length > 0)
{
string folderPath = System.Web.HttpContext.Current.Server.MapPath(FolderName);
if (!System.IO.Directory.Exists(folderPath))
{
System.IO.Directory.CreateDirectory(folderPath);
return true;
}
else
return false;
}
else
return false;
}
catch (Exception ex)
{
return false;
}
finally { }
}
//删除整个文件夹以及整个子文件夹和文件
public static bool DeleteDirctory(string folderName)
{
try
{
string filePath = System.Web.HttpContext.Current.Server.MapPath(folderName);
if (System.IO.Directory.Exists(filePath))
{
System.IO.Directory.Delete(filePath);
return true;
}
else
return false;
}
catch (Exception ex)
{
return false;
}
finally
{
}
}
//遍历一个目录下的全部目录
public static DataTable GetDir(string foldername)
{
try
{
DataTable dt = new DataTable();
dt.Columns.Add("name", typeof(string));
dt.Columns.Add("fullName", typeof(string));
string filePath = System.Web.HttpContext.Current.Server.MapPath(foldername);
System.IO.DirectoryInfo dir = new DirectoryInfo(filePath);
foreach (System.IO.FileInfo dchild in dir.GetFiles("*"))
{
DataRow dr = dt.NewRow();
dr["name"] = dchild.Name; //目录名
dr["fullname"] = dchild.FullName;
dt.Rows.Add(dr);
}
return dt;
}
catch (Exception ex)
{
return null;
}
finally { }
}
- c#对文件流的操作
- C#对文件的操作
- C#对文件的操作
- C#对文件的操作
- c# 对文件的操作
- C#对文件的操作
- C#对文件的操作
- C# 对文件操作
- C# 对文件操作
- c#中对文件的操作小结
- c#中对文件的操作小结
- C#对XML文件的操作
- c#中对文件的操作小结
- C# 对文件的操作小结
- C#中对文件的操作小结
- 关于C#对文件的操作
- c#中对文件的操作小结
- C#对Excel文件的操作
- c#获得图片的宽、高 和字节数
- 有关CA
- OGNL
- 关于OGNL的问题
- 多线程编程之一——问题提出
- c#对文件流的操作
- 标程: 浏览文件夹&对文件夹的处理
- 多线程编程之二——MFC中的多线程开发
- 多表头DBGrid
- 《谋划》二度(1)
- 多线程编程之三——线程间通讯
- Apache+php+mysql在windows下的安装与配置图解
- 多线程编程之四——线程的同步
- 从黑客角度检验Oracle数据库的安全性