2009-11-10 109 views

回答

11
public static byte[] ReadResource(string resourceName) 
{ 
    using (Stream s = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) 
    { 
     byte[] buffer = new byte[1024]; 
     using (MemoryStream ms = new MemoryStream()) 
     { 
      while (true) 
      { 
       int read = s.Read(buffer, 0, buffer.Length); 
       if (read <= 0) 
        return ms.ToArray(); 
       ms.Write(buffer, 0, read); 
      } 
     } 
    } 
} 
+0

我想閱讀我們的鏈接並在項目名爲Resources的文件夾中添加的文件。使用此函數保持導致對象引用錯誤 – williamtroup 2009-11-11 08:48:26

+0

然後使用資源而不是當前執行上下文加載程序集。 – Anton 2009-11-12 05:23:31

1

或者在你的資源類添加此

internal static byte[] GetResource(string fileName) { 
    object obj = ResourceManager.GetObject(fileName, resourceCulture); 
    return ((byte[])(obj)); 
} 
+2

此解決方案不起作用,至少當obj是BitMap時它不起作用。 – Maciej 2012-10-25 19:55:55