您的位置:首页 > 编程语言 > C#

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
%>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: