FileInfo对象OpenRead()的时候发生异常,急!

gwang119 2008-07-17 12:20:46
FileInfo fi = new FileInfo(file)
FileStream fs = fi.OpenRead();


文件存在,但是OpenRead时候出异常:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
at System.IO.FileInfo.OpenRead()

什么原因?大家帮帮忙,谢谢
...全文
418 14 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
14 条回复
切换为时间正序
请发表友善的回复…
发表回复
一品梅 2008-07-17
  • 打赏
  • 举报
回复
try
{}
catch(Exception e)
{
response.write(e.ToString);
}
gwang119 2008-07-17
  • 打赏
  • 举报
回复
全部的异常信息我都写出来了~
ericzhangbo1982111 2008-07-17
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 fgq841103 的回复:]
FileInfo的OpenRead()方法可能会抛出一下三种类型的异常:
UnauthorizedAccessException
path 为只读,或者是一个目录。

DirectoryNotFoundException
指定的路径无效,比如在未映射的驱动器上。

IOException
该文件已打开。



建议楼主好好看看出现的异常信息,确定异常错误的类型。
[/Quote]
宝_爸 2008-07-17
  • 打赏
  • 举报
回复
异常信息中应该有原因。
jzywh 2008-07-17
  • 打赏
  • 举报
回复
try
{
....
}
catch(Exception e)
{
Response.Write(e.Message);
}
xbfitliu 2008-07-17
  • 打赏
  • 举报
回复
file 如果这个不是文件路径,建议把它写成文件的实际路径。比如:C:\
哈哈哈啊002 2008-07-17
  • 打赏
  • 举报
回复
FileInfo的OpenRead()方法可能会抛出一下三种类型的异常:
UnauthorizedAccessException
path 为只读,或者是一个目录。

DirectoryNotFoundException
指定的路径无效,比如在未映射的驱动器上。

IOException
该文件已打开。



建议楼主好好看看出现的异常信息,确定异常错误的类型。

gwang119 2008-07-17
  • 打赏
  • 举报
回复
找到原因了,因为XslCompiledTransform时候,用StreamWriter加载的文件,但是StreamWriter没有close掉,到这个进程不能访问这个文件,我太笨了,没看到~呵呵,谢谢!
yagebu1983 2008-07-17
  • 打赏
  • 举报
回复
文件路径正确不?
yingziming 2008-07-17
  • 打赏
  • 举报
回复
麻雀虽小
zzyhuian06142 2008-07-17
  • 打赏
  • 举报
回复
为什么不直接用
FileStream _fs =new FileStream(path,FimeMode.Open,FileAccept.Read)呢
zzyhuian06142 2008-07-17
  • 打赏
  • 举报
回复
'D:\temp\StyleConvert\54_T_KT_01_testfile.xml' because it is being used by another process
这个文件有人在用了
gwang119 2008-07-17
  • 打赏
  • 举报
回复
我这个文件是刚刚新建的文件,XslCompiledTransform 转换过来的。难道转换还没有完成?
gwang119 2008-07-17
  • 打赏
  • 举报
回复
看到异常:

System.IO.IOException: The process cannot access the file 'D:\temp\StyleConvert\54_T_KT_01_testfile.xml' because it is being used by another process.

为什么不能访问这个文件呢?
怎样能查明不能访问的原因?
请大家指点……

111,094

社区成员

发帖
与我相关
我的任务
社区描述
.NET技术 C#
社区管理员
  • C#
  • AIGC Browser
  • by_封爱
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

让您成为最强悍的C#开发者

试试用AI创作助手写篇文章吧