从资源中复制文件

时间:2012-01-10 03:03:44

标签: c# xml file resources copy

那么在我的C#项目中,我将一个.xml文件添加到资源中,我希望将它从中提取/复制到应用程序路径,我试图这样做:

string appPath = Path.GetDirectoryName(Application.ExecutablePath);//Declaration of the apppath
File.Copy(appPath, Properties.Resources.config);//process for copy

但是不行:/,我怎么能做我想做的事?

1 个答案:

答案 0 :(得分:6)

确保资源上的构建操作设置为“embed resource”。

var assembly = Assembly.GetExecutingAssembly();
// See what resources are in the assembly (remove from the final code)
foreach (var name in assembly.GetManifestResourceNames()) {
    Console.Writeline("'{0}'", name);
}
using (var inputStream = assembly.GetManifestResourceStream(resourcePath)) {
    using( var outStream = File.OpenWrite(copyToPath)) {
        input.CopyTo(outStream);
    }
}