.NET CF中的FileStream和ObjectDisposedException

时间:2012-04-17 09:55:36

标签: c# compact-framework filestream objectdisposedexception

public byte[] GetFile(string filename)
{
    FileStream aStream = File.Open(filename, FileMode.Open, FileAccess.Read);
    BinaryReader binReader = new BinaryReader(aStream);
    binReader.BaseStream.Position = 0;
    byte[] binFile = binReader.ReadBytes(Convert.ToInt32(binReader.BaseStream.Length));
    binReader.Close();
    return binFile;
}

我为许多文件路径运行此方法,问题是无法使用File.Open访问文件(因为它被另一个进程使用)我得到:

'aStream.Position' threw an exception of type 'System.ObjectDisposedException'

在以下一行:

binReader.BaseStream.Position = 0;

我很少得到

{System.IO.IOException: The process can not access the file '\folder\file.txt' because it is being used by another process.}

这是我想要的例外。那么为什么物体大多数时间处理? 注意:我首先在using语句中使用了FileStream行,但删除了它,因为我认为可能已经放置了该对象。但问题仍然存在。

编辑:使用Compact Framework,它没有ReadAllBytes

3 个答案:

答案 0 :(得分:2)

可能在您的文件正在使用时FileStream投放IOException的部分时间,以及其他时间,或许 ,你得到的是ObjectDisposedException,因为你的数组没有被初始化。

显然,我无法测试这个理论。

看看你是否可以复制粘贴这个效果好的结果:

public byte[] GetFile(string filename)
{
  byte[] binFile = null;
  try
  {
    using (var aStream = File.Open(filename, FileMode.Open, FileAccess.Read))
    {
      BinaryReader binReader = new BinaryReader(aStream);
      binFile = new byte[binReader.BaseStream.Length];
      binReader.BaseStream.Position = 0; // <= this step should not be necessary
      binFile = binReader.ReadBytes(binReader.BaseStream.Length);
      binReader.Close();
    }
  } catch (IOException err) {
    // file is being used by another process.
  } catch (ObjectDisposedException err) {
    // I am guessing you would never see this because your binFile is not disposed
  }
  return binFile;
}

请务必检查空返回变量!

修改

我写了(我认为是)一个更简单的版本。我测试了它,似乎工作正常。我也更喜欢Read()重载到ReadBytes(),因为我知道有多少数据被吸入。

首先,是为我的Pictures文件夹中的每个图像调用方法的测试函数:

public void Test() {
  DirectoryInfo dir = new DirectoryInfo(Environment.GetFolderPath(Environment.SpecialFolder.Personal));
  foreach (var subDir in dir.GetDirectories()) {
    if (-1 < subDir.Name.ToLower().IndexOf("pictures")) {
      foreach (var file in subDir.GetFiles()) {
        byte[] data = GetFile(file.FullName);
        if (data != null) {
          Console.WriteLine(data.Length);
        }
      }
    }
  }
}

public byte[] GetFile(string filename) {
  byte[] result = null;
  try {
    if (File.Exists(filename)) {
      int len = 0;
      FileInfo file = new FileInfo(filename);
      byte[] data = new byte[file.Length];
      using (BinaryReader br = new BinaryReader(file.Open(FileMode.Open, FileAccess.Read))) {
        len = br.Read(data, 0, data.Length);
        br.Close();
      }
      if (0 < len) {
        if (len == data.Length) {
          return data;
        } else {
          // this section of code was never triggered in my tests;
          // however, it is good to keep it as a backup.
          byte[] dat2 = new byte[len];
          Array.Copy(data, dat2, len);
          return dat2;
        }
      }
    }
  } catch (IOException err) {
    // file is being used by another process.
  } catch (ObjectDisposedException err) {
    // I am guessing you would never see this because your binFile is not disposed
  }
  return result;
}

除非你有int溢出,否则我认为没有任何理由不起作用。

答案 1 :(得分:1)

为什么不简单地使用

public byte[] GetFile(string filename)
{
    try { return File.ReadAllBytes(filename); }
    catch { return null; }
}

只是为了好玩,你甚至可以定义一个扩展方法

public static class Extensions
{
    public static byte[] GetFile(this string filename)
    {
        try { return File.ReadAllBytes(filename); }
        catch { return null; }
    }
}

所以你可以做byte[] myfile = filename.GetFile(); 请记住,必须在继续之前检查返回值是否为空:

if (myfile != null)
{
    // Do what you need
}

答案 2 :(得分:1)

请使用:

byte[] contents = File.ReadAllBytes(filename);