C#文件操作(vs2005,c#,fso)
2009-02-05 14:07
453 查看
若要执行此操作... | 请参见本主题中的示例... |
---|---|
创建文本文件。 | 如何:向文件写入文本 |
写入文本文件。 | 如何:向文件写入文本 |
读取文本文件。 | 如何:从文件读取文本 |
向文件中追加文本。 | 如何:打开并追加到日志文件 File.AppendText FileInfo.AppendText |
重命名或移动文件。 | File.Move FileInfo.MoveTo |
删除文件。 | File.Delete FileInfo.Delete |
复制文件。 | File.Copy FileInfo.CopyTo |
获取文件大小。 | FileInfo.Length |
获取文件属性。 | File.GetAttributes |
设置文件属性。 | File.SetAttributes |
确定文件是否存在。 | File.Exists |
读取二进制文件。 | 如何:对新建的数据文件进行读取和写入 |
写入二进制文件。 | 如何:对新建的数据文件进行读取和写入 |
检索文件扩展名。 | Path.GetExtension |
检索文件的完全限定路径。 | Path.GetFullPath |
检索路径中的文件名和扩展名。 | Path.GetFileName |
更改文件扩展名。 | Path.ChangeExtension |
C#文件操作(vs2005,c#,fso)
using System;using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
/// <summary>
/// FileOperate 的摘要说明
/// </summary>
public class FileOperate
{
public FileOperate()
{
//
// TODO: 在此处添加构造函数逻辑
//
}
//生成一个文件
public static bool WriteFile(string FileContent,string FilePath)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
if(System.IO.File.Exists(strPath))
{
System.IO.File.Delete(strPath);
}
System.IO.FileStream Fs = System.IO.File.Create(strPath);
byte[] bcontent = System.Text.Encoding.GetEncoding("GB2312").GetBytes(FileContent);
Fs.Write(bcontent,0,bcontent.Length);
Fs.Close();
Fs = null;
return true;
}
catch
{
return false;
}
finally
{
}
}
//读入文件
public static string ReadWrite(string FilePath)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath);
if (System.IO.File.Exists(strPath))
{
string strRead = System.IO.File.ReadAllText(strPath, System.Text.Encoding.GetEncoding("gb2312"));
return strRead;
}
else
{
return "false";
}
}
catch
{
return "false";
}
finally
{
}
}
//向文件中
public static bool UpdateFile(string FilePath, string FileContent)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
if (!System.IO.File.Exists(strPath))
{
//文件不存在,生成文件
System.IO.FileStream Fs = System.IO.File.Create(strPath);
byte[] bcontent = System.Text.Encoding.GetEncoding("GB2312").GetBytes(FileContent);
Fs.Write(bcontent, 0, bcontent.Length);
Fs.Close();
Fs = null;
return true;
}
System.IO.FileStream FileRead = new System.IO.FileStream(strPath, System.IO.FileMode.Open, System.IO.FileAccess.ReadWrite);
System.IO.StreamReader FileReadWord = new System.IO.StreamReader(FileRead, System.Text.Encoding.Default);
string OldString = FileReadWord.ReadToEnd().ToString();
OldString = OldString + FileContent;
//把新的内容重新写入
System.IO.StreamWriter FileWrite = new System.IO.StreamWriter(FileRead, System.Text.Encoding.Default);
FileWrite.Write(OldString);
//关闭
FileWrite.Close();
FileReadWord.Close();
FileRead.Close();
return true;
}
catch
{
// throw;
return false;
}
}
//删除文件
public static bool DeleteFile(string FilePath)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
if (System.IO.File.Exists(strPath))
{
System.IO.File.Delete(strPath);
}
return true;
}
catch
{
return false;
}
finally
{
}
}
//创建文件夹
public static bool CreateFolder(string FolderName)
{
if (FolderName.Trim().Length > 0)
{
try
{
string FolderPath = System.Web.HttpContext.Current.Server.MapPath(FolderName);
if (!System.IO.Directory.Exists(FolderPath))
{
System.IO.Directory.CreateDirectory(FolderPath);
return true;
}
else
{
return true;
}
}
catch
{
return false;
}
finally
{
}
}
else
{
return false;
}
}
//删除整个文件夹及其字文件夹和文件
public static bool DeleteFolder(string FolderName)
{
if (FolderName.Trim().Length > 0)
{
try
{
string FolderPath = System.Web.HttpContext.Current.Server.MapPath(FolderName);
if (System.IO.Directory.Exists(FolderPath))
{
System.IO.Directory.Delete(FolderPath,true);
return true;
}
else
{
return true;
}
}
catch
{
return false;
}
finally
{
}
}
else
{
return false;
}
}
// 删除整个文件夹及其字文件夹和文件 (验证没成功)
public static bool DeleParentFolder(string FolderPathName)
{
try
{
string strPath = System.Web.HttpContext.Current.Server.MapPath(FolderPathName).ToString();
System.IO.DirectoryInfo DelFolder = new System.IO.DirectoryInfo(strPath);
if (DelFolder.Exists)
{
DelFolder.Delete();
}
return true;
}
catch
{
return false;
}
}
//遍历一个目录下的全部目录
public static DataTable GetDir(string FilePath)
{
DataTable tb = new DataTable();
tb.Columns.Add("name",typeof(string));
tb.Columns.Add("fullname", typeof(string));
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(strPath);
foreach ( System.IO.DirectoryInfo dChild in dir.GetDirectories("*"))
{
//如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
DataRow Dr=tb.NewRow();
Dr["name"] = dChild.Name; //打印目录名
Dr["fullname"] = dChild.FullName; //打印路径和目录名
tb.Rows.Add(Dr);
}
return tb;
}
//遍历一个目录下的全部目录
public static DataTable GetFile(string FilePath)
{
DataTable tb = new DataTable();
tb.Columns.Add("name", typeof(string));
tb.Columns.Add("fullname", typeof(string));
string strPath = System.Web.HttpContext.Current.Server.MapPath(FilePath).ToString();
System.IO.DirectoryInfo dir = new System.IO.DirectoryInfo(strPath);
foreach (System.IO.FileInfo dChild in dir.GetFiles("*"))
{
//如果用GetDirectories("ab*"),那么全部以ab开头的目录会被显示
DataRow Dr = tb.NewRow();
Dr["name"] = dChild.Name; //打印目录名
Dr["fullname"] = dChild.FullName; //打印路径和目录名
tb.Rows.Add(Dr);
}
return tb;
}
}
相关文章推荐
- C#文件操作
- C#对文件的操作
- C#操作Word文件
- C# 文件操作方法大全(转载)
- C#操作文件之 按行写入文件实例
- 【整理】C#文件操作大全(SamWang)
- C#操作目录与文件的方法步骤
- C#中的文件操作
- C#文件操作大全
- 文件操作c/c++/c#
- C#文件相关操作
- C#中文件与文件夹相关的操作
- C#操作mdb数据库文件
- C#文件、目录操作相关函数
- c# .Net :Excel NPOI导入导出操作教程之数据库表信息数据导出到一个Excel文件并写到磁盘示例分享
- 【整理】C#文件操作大全(SamWang)
- C#文件的拆分与合并操作示例
- C#文件读写IO操作 实例
- 【转】c#文件操作大全(一)
- 下载 拷贝文件 (C# VS2005)备忘的-_-