0

I have an application that uses a template file and a CSV file. It works perfectly fine since I do have the said files on my computer and I am referencing their paths. The only thing I want to do is that when the software is published and installed (and the said files are in the Resources folder so it would make them an embedded resource), the csv and template files would be copied to a directory folder that my program would use. Preferably the paths it would be copied to would be something like this : "C:\FILES" + template.dotx .

Now how do I get/retrieve the said files from the Resources Folder in my software into a new folder?

4

1 回答 1

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

复制流方法:

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);
    }
}
于 2012-11-04T06:58:55.240 回答