会员
周边
众包
新闻
博问
闪存
所有博客
当前博客
我的博客
我的园子
账号设置
简洁模式
...
退出登录
注册
登录
沪牌学院
沪拍拍课堂
博客园
新随笔
管理
文件创建及读取的方法
文件创建及读取的方法。 使用VSS对签入的文件进行操作,会报错"文件正由另一进程使用,因此该进程无法访问该文件。"
以前创建用
String filePath
=
HttpContext.Current.Server.MapPath(FileName);
if
(
!
System.IO.File.Exists(filePath))
//
创建文件
System.IO.File.Create(filePath);
System.IO.StreamWriter sw
=
new
System.IO.StreamWriter(filePath,
false
);
sw.WriteLine(html);
sw.Close();
读取用
if
(System.IO.File.Exists(filePath))
{
//
System.IO.FileStream fs = System.IO.File.OpenRead(filePath);
FileStream fs
=
new
FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.Read);
StreamReader sr
=
new
StreamReader(filePath, System.Text.Encoding.UTF8);
//
StreamReader sr = new StreamReader(filePath);
while
(sr.Peek()
>
-
1
)
//
StreamReader.Peek()返回下一个可用字符,但不使用它
{
Response.Write(sr.ReadLine());
}
sr.Close();
fs.Close();
}
如果文件不存在的话,就会有
文件“G:\wwwRoot\wufengBS\文件处理\code.xls”正由另一进程使用,因此该进程无法访问该文件。
的错误。
改用流创建即可:
if
(
!
System.IO.File.Exists(filePath))
//
创建文件
{
System.IO.FileStream fs
=
System.IO.File.Create(filePath);
fs.Close();
}
posted @
2006-11-25 09:25
无锋不起浪
阅读(
1781
) 评论(
3
)
编辑
收藏
举报
会员力量,点亮园子希望
刷新页面
返回顶部