2010-04-08 23 views
1

我是新來的境界,在.NET中對文件進行處理.NET使嵌入文件資源的副本到本地驅動器

我創建與3.5框架在VB.NET WPF應用程序。 (如果你在C#中提供了一個例子,那很好。)

在我的項目中,我有一個用於MS Access數據庫的模板。我希望的行爲是,當用戶單擊文件 - >新建時,他們可以創建此模板的新副本,爲其指定文件名,並將其保存到本地目錄中。

數據庫已經擁有的表,並與我的應用程序(一個用戶友好的數據編輯器)

接口所​​需要的一些數據開始我想的辦法是包括這個「template.accdb」文件作爲資源在項目中,並在運行時以某種方式將其寫入文件?

任何指導將非常非常感激。

謝謝!

回答

5

一定要在完全合格的命名空間名稱傳遞給資源名稱

Public Function ExtractResourceToDisk(ByVal ResourceName As String, ByVal FileToExtractTo As String) As Boolean 

    Dim s As System.IO.Stream = System.Reflection.Assembly.GetExecutingAssembly.GetManifestResourceStream(ResourceName) 
    Dim ResourceFile As New System.IO.FileStream(FileToExtractTo, IO.FileMode.Create) 

    Dim b(s.Length) As Byte 

    s.Read(b, 0, s.Length) 
    ResourceFile.Write(b, 0, b.Length - 1) 
    ResourceFile.Flush() 
    ResourceFile.Close() 

    ResourceFile = Nothing 
End Function 
+0

美麗的解決方案!它在第一次嘗試中工作。你們是如此聰明:p – 2010-04-08 23:58:09

2

那麼「最簡單」的事情就是將它作爲輸出文件包含在程序安裝中,以便在應用程序執行時(可能位於應用程序目錄或其子目錄中)安裝。然後,您可以在需要創建新模板時簡單地使用File.Copy。我不主張這是最好的解決方案,只是最少的工作量。您可以閱讀File.Copy方法here

2
public static void WriteResourceToFile(Assembly assembly, Type scope, string resname, string path) 
{ 
    Stream res = assembly.GetManifestResourceStream(scope, resname); 
    using (FileStream file = new FileStream(path, FileMode.Create)) 
    { 
     byte[] buffer = new byte[4096]; 
     int bytesRead; 
     while ((bytesRead = res.Read(buffer, 0, buffer.Length)) > 0) 
     { 
      file.Write(buffer, 0, bytesRead); 
     } 
    } 
} 

應該這樣做。其中scope是資源名稱的命名空間範圍,它允許您縮短資源名稱,這是完全限定的。

0

我修改了Greg Bogumil從上面的答案來創建擴展方法。 現在任何二進制類型的資源文件,我可以簡單的做這樣的事情......

My.Resources.some_resource_file.ExtractResourceToDisk(newpath)

注意,您必須將以下函數添加到模塊,您不能創建一個類內部的擴展方法。

''' <summary> 
''' Extracts the binary resource file and saves it to the specified location. 
''' </summary> 
''' <param name="Resource">[Byte()] The binary resource file byte array.</param> 
''' <param name="FileToExtractTo">[String] The full file path of the new file to be saved.</param> 
''' <returns>[Boolean] Returns True on success.</returns> 
<Extension> 
Public Function ExtractResourceToDisk(ByVal Resource As Byte(), ByVal FileToExtractTo As String) As Boolean 
    Try 
     Using ms As New MemoryStream(Resource) 
      Using ResourceFile As New FileStream(FileToExtractTo, FileMode.Create) 
       Dim b(ms.Length) As Byte 
       ms.Read(b, 0, ms.Length) 
       ResourceFile.Write(b, 0, b.Length - 1) 
       ResourceFile.Flush() 
       ResourceFile.Close() 
      End Using 
     End Using 
     Return True 
    Catch ex As Exception 
     Return False 
    End Try 
End Function 
0

首先,請務必將設置爲Embedded resource相應文件的Build Action

然後根據@格雷格博古米爾答案,運行此方法:

public static bool ExtractResourceToDisk(string ResourceName, string FileToExtractTo) { 
    //Choose any one assembly that matches your needs. 
    var asm = System.Reflection.Assembly.GetEntryAssembly(); 
    //var asm = System.Reflection.Assembly.GetExecutingAssembly(); 
    //var asm = System.Reflection.Assembly.GetCallingAssembly(); 
    var s = asm.GetManifestResourceStream(ResourceName); 
    var resFile = new System.IO.FileStream(FileToExtractTo, System.IO.FileMode.Create); 
    if (resFile == null) return false; 
    s.CopyTo(resFile); 
    return true; 
} 

我用的是CopyTo方法,而不是因爲它更簡潔。

相關問題