高手们,为什么我把服务器的一个文件读取成byte,关闭所有程序,用手工操作要删掉那个文件,文件有一个进程正在使用,请先关闭进程以下是我读文件的代码:
[WebMethod(Description="Web 服务提供的方法,返回给定文件的字节数组")]
public byte[] getfilename(string requestFileName)
{
///得到服务器端的一个文件
if(requestFileName == null || requestFileName == "")
return new byte[0];
else
return getBinaryFile(Server.MapPath(".") + "\\"
+ requestFileName);
}
public byte[] getBinaryFile(string filename)
{
if(File.Exists(filename))
{
try
{
///打开现有文件以进行读取。
FileStream s = File.OpenRead(filename);
return ConvertStreamToByteBuffer(s);
s.Close();
}
catch(Exception e)
{
return new byte[0];
}
}
else
{
return new byte[0];
}
} public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while((b1=theStream.ReadByte())!=-1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
tempStream.Close();
}
[WebMethod(Description="Web 服务提供的方法,返回给定文件的字节数组")]
public byte[] getfilename(string requestFileName)
{
///得到服务器端的一个文件
if(requestFileName == null || requestFileName == "")
return new byte[0];
else
return getBinaryFile(Server.MapPath(".") + "\\"
+ requestFileName);
}
public byte[] getBinaryFile(string filename)
{
if(File.Exists(filename))
{
try
{
///打开现有文件以进行读取。
FileStream s = File.OpenRead(filename);
return ConvertStreamToByteBuffer(s);
s.Close();
}
catch(Exception e)
{
return new byte[0];
}
}
else
{
return new byte[0];
}
} public byte[] ConvertStreamToByteBuffer(System.IO.Stream theStream)
{
int b1;
System.IO.MemoryStream tempStream = new System.IO.MemoryStream();
while((b1=theStream.ReadByte())!=-1)
{
tempStream.WriteByte(((byte)b1));
}
return tempStream.ToArray();
tempStream.Close();
}
解决方案 »
- 只求一个简单的进度条实现,能帮我结解问题我就把分全给他~~急。
- 一个文本编辑器的标志功能怎样实现?
- C#项目启动的问题
- 请求帮助,希望有人帮忙修改下代码,看看问题出在哪里,这是一个实现拼音过滤功能的自定义ComboBox
- DataSet 数据导出为用那种文件格式好?
- 如何捕获指定进程开始运行?(不使用Timer)
- 关于用WINFORM实现欢迎窗口淡入淡出的问题~!在线等!急~!
- 小妹妹急救:总是报错:未将对象引用设置到对象的实例
- 有不懂的问题 是C#的 大家帮帮忙啊
- 这是什么原因引起的呀?
- 在问:Enterprise Library2.0那有下载!!!
- 在IE的工具栏区插入了一个用户控件,在IE的浏览区也插入了一个用户控件。怎么使工具栏区的用户控件的click事件使浏览区中的用户控件显示
s.Close();
---------
已经return了,没执行
s.Close();可以这样
byte[] temp = ConvertStreamToByteBuffer(s);
s.Close();
return remp;
FileStream fs = new FileStream(filePath, FileMode.Open,FileAccess.Read);
byte[] desBytes = new byte[fs.Length];
fs.Read(desBytes, 0, desBytes.Length);
fs.Close();
这样就可以了