2012-11-04 53 views
0

我有一個使用模板文件和CSV文件的應用程序。它工作得很好,因爲我的計算機上有這些文件,而且我正在引用它們的路徑。我想要做的唯一事情是當軟件發佈和安裝(並且所述文件位於Resources文件夾中以便它們成爲嵌入資源時),csv和模板文件將被複制到我的目錄文件夾中程序會使用。最好將其複製到的路徑將如下所示:「C:\ FILES」+ template.dotx。如何從C#中的資源文件夾檢索.dotx和.csv文件?

現在如何從我的軟件的資源文件夾中獲取/檢索所述文件到新文件夾中?

回答

2

你可以稱之爲

System.Reflection.Assembly.GetExecutingAssembly().GetManifestResourceNames(); 

,並檢查其嵌入的資源都可以訪問。然後你可以比較一下你正在通過的內容,看看你是否確實完成了你的預期。

string FileExtractTo = "C:\FILES"; 
DirectoryInfo dirInfo = new DirectoryInfo(FileExtractTo); 

if (!dirInfo.Exists()) 
    dirInfo.Create(); 

using (Stream input = Assembly.GetExecutingAssembly().GetManifestResourceStream(resourceName)) 
using (Stream output = File.Create(FileExtractTo + "\template.dotx")) 
{ 
    CopyStream(input, output); 
} 

CopyStream方法:

public static void CopyStream(Stream input, Stream output) 
{ 
    // Insert null checking here for production 
    byte[] buffer = new byte[8192]; 

    int bytesRead; 
    while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0) 
    { 
     output.Write(buffer, 0, bytesRead); 
    } 
} 
+0

將這個罵人的字符串數組? System.Reflection.Assembly.GetExecutingAssembly()GetManifestResourceNames(); –

+0

是的,這會以字符串[]的形式返回所有的資源,然後你可以調用GetManifestResourceStream(resourceName);獲取嵌入式資源的流。 –

+0

看到我編輯的答案。希望這可以幫助! –

相關問題