c#文件操作
2009-02-04 13:23
288 查看
c#文件操作
FileSystemObject 包含了处理文件系统的方法
FileSystemObject:包含了处理文件系统的所有基本方法。
TextStream:用来读写文本文件。
File:此对象的方法和属性用于处理单个文件 。
Folder:此对象的方法和属性用于处理文件夹。
Drive:表示磁盘驱动器或网络共享。
.write写入字符
.writeline写入一行
1
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>create text</title>
</head>
<body>
<%
set fs=server.CreateObject("scripting.FileSystemObject")
set file1=fs.CreateTextFile(server.MapPath("/testasp/test.txt"))
file1.writeline("Hello")
file1.close
%>
</body>
</html>
2
<head><title>追加文件</title></head>
<body>
追加数据…
<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
set file1 = fs.OpenTextFile(Server.MapPath("/testasp/test.txt"),8,true)
for i=100 to 102
file1.WriteLine(CStr(i) & ". 追加的新行。")
next
file1.close
%>
</body>
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>create text</title>
</head>
<body>
<%
set fs=server.CreateObject("scripting.FileSystemObject")
set file1=fs.CreateTextFile(server.MapPath("/testasp/test.txt"))
for i=0 to 10
file1.writeline(Cstr(i)&".Hello")
next
file1.close
%>
</body>
</html>
<head><title>读文件</title></head>
<body>
<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
set file1 = fs.OpenTextFile(Server.MapPath("/testasp/test.txt"),1,true)
Do While not file1.AtEndOfStream
Response.Write file1.ReadLine & "<br>"
loop
file1.close
%>
</body>
//////
CreateTextFile(FileSpecifier,overwrite,unicode)
FileSpecifier:路径
overwrite:是否覆盖原有文件 默认为true
unicode:默认为:false (asciI码)
////////////////
读
OpenTextFile(FileSpecifier,mode,create,format)
mode:可选 1打开文件进行读取(不能写入)
2打开文件进行写入
3追加
create:可选true 表示创建新文件 false为默认值
format:设置文件格式(可选)
0 ASCII打开
-1 unicode打开
-2 使用系统默认值打开
FileSystemObject 对象的 MoveFile 方法可将一个或多个文件从一个位置移动到另一个位置。
FileSystemObject.MoveFile source,destination
<head><title>移动文件</title></head>
<body>
Drive对象
<html>
<head><title>驱动器列表</title></head>
<body>
<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
for each thing in fs.Drives
response.write "<br>驱动器字母:"& thing.DriveLetter
response.write "<br>驱动器总字节数:"& thing.TotalSize
response.write "<br>驱动器可用空间:"& thing.AvailableSpace
response.write "<hr>"
next
%>
</body>
</html>
创建文件夹
<%
Set fs =
Server.CreateObject("Scripting.FileSystemObject")
fs.CreateFolder("c:/asp")
%>
复制文件夹
<%
'将文件夹 NewFiles 从 C:/复制到文件夹 c:/Files目录下
Set fs =
Server.CreateObject("Scripting.FileSystemObject")
fs.CopyFolder "C:/NewFiles","C:/Files/ "
%>
检查文件夹是否存在
<%
Set fs =
Server.CreateObject("Scripting.FileSystemObject")
If fs.FolderExists("c:/asp")=true then
Response.Write(“文件夹 c:/asp 存在!”)
Else
Response.Write(“文件夹 c:/asp 不存在!”)
End if
%>
移动文件夹
<%
Set fs=
Server.CreateObject("Scripting.FileSystemObject")
fs.MoveFolder "c:/NewFiles","c:/VeryNewFiles/ "
%>
删除文件夹
<%
Set fs =
Server.CreateObject("Scripting.FileSystemObject")
If fs.FolderExists("c:/Files/NewFiles")
then
fs.DeleteFolder("c:/Files/NewFiles")
End if
%>
FileSystemObject 包含了处理文件系统的方法
FileSystemObject:包含了处理文件系统的所有基本方法。
TextStream:用来读写文本文件。
File:此对象的方法和属性用于处理单个文件 。
Folder:此对象的方法和属性用于处理文件夹。
Drive:表示磁盘驱动器或网络共享。
.write写入字符
.writeline写入一行
1
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>create text</title>
</head>
<body>
<%
set fs=server.CreateObject("scripting.FileSystemObject")
set file1=fs.CreateTextFile(server.MapPath("/testasp/test.txt"))
file1.writeline("Hello")
file1.close
%>
</body>
</html>
2
<head><title>追加文件</title></head>
<body>
追加数据…
<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
set file1 = fs.OpenTextFile(Server.MapPath("/testasp/test.txt"),8,true)
for i=100 to 102
file1.WriteLine(CStr(i) & ". 追加的新行。")
next
file1.close
%>
</body>
<%@LANGUAGE="VBSCRIPT" CODEPAGE="936"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>create text</title>
</head>
<body>
<%
set fs=server.CreateObject("scripting.FileSystemObject")
set file1=fs.CreateTextFile(server.MapPath("/testasp/test.txt"))
for i=0 to 10
file1.writeline(Cstr(i)&".Hello")
next
file1.close
%>
</body>
</html>
<head><title>读文件</title></head>
<body>
<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
set file1 = fs.OpenTextFile(Server.MapPath("/testasp/test.txt"),1,true)
Do While not file1.AtEndOfStream
Response.Write file1.ReadLine & "<br>"
loop
file1.close
%>
</body>
//////
CreateTextFile(FileSpecifier,overwrite,unicode)
FileSpecifier:路径
overwrite:是否覆盖原有文件 默认为true
unicode:默认为:false (asciI码)
////////////////
读
OpenTextFile(FileSpecifier,mode,create,format)
mode:可选 1打开文件进行读取(不能写入)
2打开文件进行写入
3追加
create:可选true 表示创建新文件 false为默认值
format:设置文件格式(可选)
0 ASCII打开
-1 unicode打开
-2 使用系统默认值打开
FileSystemObject 对象的 MoveFile 方法可将一个或多个文件从一个位置移动到另一个位置。
FileSystemObject.MoveFile source,destination
<head><title>移动文件</title></head>
<body>
Drive对象
<html>
<head><title>驱动器列表</title></head>
<body>
<%
set fs=Server.CreateObject("Scripting.FileSystemObject")
for each thing in fs.Drives
response.write "<br>驱动器字母:"& thing.DriveLetter
response.write "<br>驱动器总字节数:"& thing.TotalSize
response.write "<br>驱动器可用空间:"& thing.AvailableSpace
response.write "<hr>"
next
%>
</body>
</html>
创建文件夹
<%
Set fs =
Server.CreateObject("Scripting.FileSystemObject")
fs.CreateFolder("c:/asp")
%>
复制文件夹
<%
'将文件夹 NewFiles 从 C:/复制到文件夹 c:/Files目录下
Set fs =
Server.CreateObject("Scripting.FileSystemObject")
fs.CopyFolder "C:/NewFiles","C:/Files/ "
%>
检查文件夹是否存在
<%
Set fs =
Server.CreateObject("Scripting.FileSystemObject")
If fs.FolderExists("c:/asp")=true then
Response.Write(“文件夹 c:/asp 存在!”)
Else
Response.Write(“文件夹 c:/asp 不存在!”)
End if
%>
移动文件夹
<%
Set fs=
Server.CreateObject("Scripting.FileSystemObject")
fs.MoveFolder "c:/NewFiles","c:/VeryNewFiles/ "
%>
删除文件夹
<%
Set fs =
Server.CreateObject("Scripting.FileSystemObject")
If fs.FolderExists("c:/Files/NewFiles")
then
fs.DeleteFolder("c:/Files/NewFiles")
End if
%>
相关文章推荐
- c# 操作 xml 文件的 自定义类
- C#学习第十天 文件操作、文件流
- C# 文件的一些基本操作
- c#文件操作大全(二)
- c#操作xml文件之二
- C#的二进制文件操作及汉字编码转换
- C#操作目录和文件讲解
- C# 文件操作,遍历文件夹、读文件、写入、修改、复制
- C#操作配置文件(增、删、改、查)
- C#中的文件操作大全
- C#学习与上位机开发之文件操作(EXCEL保存案例)
- C# 文件操作
- C#文件操作
- C#对文件删除,移动,添加。。。操作
- 30天C#基础巩固------集合,File(文件操作 ),Encoding处理字符集
- C# 文件操作
- 在aspx文件里写C#代码(各种插入,选择,删除等操作,重要)
- C# 文件和文件夹的操作
- c#文件助手类分享(读取文件内容 操作日志文件)
- C# 用Linq的方式实现对Xml文件的基本操作(创建xml文件、增删改查xml文件节点信息)