C# System.IO.FileStream 读取被其他程序打开的文件提示“文件正由另一进程使用,因此该进程无法访问该文件。”
2010-03-16 10:51
786 查看
#region 将二进制转化为文件
public static string ConvertByteToFile(object objData, string filePathName)
{
//string fileName = "";
//fileName = new PublicConst().PathTempFile + fileName;
string folder = System.IO.Path.GetDirectoryName(filePathName);
if (!System.IO.Directory.Exists(folder))
{
System.IO.Directory.CreateDirectory(folder);
}
if (System.IO.File.Exists(filePathName))
{
try
{
System.IO.File.Delete(filePathName);
}
catch
{
//("FileInUse");
return "";
}
}
System.IO.FileStream fs = new System.IO.FileStream(filePathName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
try
{
w.Write(objData as byte[]);
}
catch (Exception)
{
}
finally
{
w.Close();
fs.Close();
}
return filePathName;
}
#endregion
将文件转化为二进制代码时,出现提示:
文件正由另一进程使用,因此该进程无法访问该文件
原来是构造System.IO.FileStream时,使用的方法有问题
一开始是直接使用
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open)
这个方法打开文件的时候是以只读共享的方式打开的,但若此文件已被一个拥有写权限的进程打开的话,就无法读取了,
因此需要使用
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open,System.IO.FileAccess.Read,FileShare.ReadWrite);
设置文件共享方式为读写,FileShare.ReadWrite,这样的话,就可以打开了
附,把二进制转化为文件的函数
这两个函数经常用来存取数据库哦的BLOB字段。
public static string ConvertByteToFile(object objData, string filePathName)
{
//string fileName = "";
//fileName = new PublicConst().PathTempFile + fileName;
string folder = System.IO.Path.GetDirectoryName(filePathName);
if (!System.IO.Directory.Exists(folder))
{
System.IO.Directory.CreateDirectory(folder);
}
if (System.IO.File.Exists(filePathName))
{
try
{
System.IO.File.Delete(filePathName);
}
catch
{
//("FileInUse");
return "";
}
}
System.IO.FileStream fs = new System.IO.FileStream(filePathName, System.IO.FileMode.Create, System.IO.FileAccess.Write);
System.IO.BinaryWriter w = new System.IO.BinaryWriter(fs);
try
{
w.Write(objData as byte[]);
}
catch (Exception)
{
}
finally
{
w.Close();
fs.Close();
}
return filePathName;
}
#endregion
将文件转化为二进制代码时,出现提示:
文件正由另一进程使用,因此该进程无法访问该文件
原来是构造System.IO.FileStream时,使用的方法有问题
一开始是直接使用
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open)
这个方法打开文件的时候是以只读共享的方式打开的,但若此文件已被一个拥有写权限的进程打开的话,就无法读取了,
因此需要使用
System.IO.FileStream fs = new System.IO.FileStream(fileName, System.IO.FileMode.Open,System.IO.FileAccess.Read,FileShare.ReadWrite);
设置文件共享方式为读写,FileShare.ReadWrite,这样的话,就可以打开了
附,把二进制转化为文件的函数
这两个函数经常用来存取数据库哦的BLOB字段。
相关文章推荐
- C# System.IO.FileStream 读取被其他程序打开的文件提示“文件正由另一进程使用,因此该进程无法访问该文件。”
- C# System.IO.FileStream 读取被其他程序打开的文件提示“文件正由另一进程使用,因此该进程无法访问该文件。”
- C# System.IO.FileStream 读取被其他程序打开的文件提示“文件正由另一进程使用,因此该进程无法访问该文件。”
- c# 读取其他程序正打开的文件的时“正由另一进程使用,因此该进程无法访问该文件。"的问题解决方法
- FileStream 读取文件时提示“xx文件正在被另一进程使用,因此该进程无法访问该文件”
- C# System.IO.FileStream 文件正由另一进程使用,因此该进程无法访问该文件
- 使用VS2010编写C#程序时总是提示正由另一进程使用,因此该进程无法访问此文件的解决办法
- C# System.IO.FileStream 文件正由另一进程使用,因此该进程无法访问该文件
- C#程序提示“正由另一进程使用,因此该进程无法访问该文件”的解决办法
- C#操作Office文件,成功释放,但读取文件时提示"文件***正由另一进程使用,因此该进程无法访问此文件"的解决办法
- 用流的方法解决“文件正由另一进程使用,因此该进程无法访问该文件” 的问题 FileStream
- IIS网站启动不了,错误提示“另一个程序正在使用此文件,进程无法访问”
- IIS错误提示:另一个程序正在使用此文件 进程无法访问
- Sytem.IO.IOException:文件“XXX”正由另一进程使用,因此该进程无法访问该文件
- IIS起用"默认网站"出现提示"另一程序正在使用此文件,进程无法访问"
- iis提示“另一个程序正在使用此文件,进程无法访问”解决办法
- windows2003 IIS 服务启动失败,提示‘另一个程序正在使用此文件,进程无法访问',解决方法!
- IIS出现“另一个程序正在使用此文件,进程无法访问。”错误提示的解决办法
- C# System.IO.FileStream 文件正由另一进程使用,因此该进程无法访问该文件
- 关于IIS服务启动失败的问题:“IIS提示‘另一个程序正在使用此文件,进程无法访问’”,的解决方法