簡體   English   中英

如何從C#中的Resources文件夾中檢索.dotx和.csv文件?

[英]How to retrieve .dotx and .csv files from Resources Folder in C#?

我有一個使用模板文件和CSV文件的應用程序。 它工作得很好,因為我在我的計算機上有所說的文件,我正在引用它們的路徑。 我唯一想做的是,當軟件發布和安裝時(並且所述文件在Resources文件夾中,這樣它們將使它們成為嵌入式資源),csv和模板文件將被復制到我的目錄文件夾中程序會用。 優選地,它將被復制到的路徑將是這樣的:“C:\\ FILES”+ template.dotx。

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

你可以打電話

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);
    }
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM