相关疑难解决方法(0)

将汇编资源流中的文件写入磁盘

我似乎无法找到一种更有效的方法将嵌入式资源"复制"到磁盘,而不是以下方法:

using (BinaryReader reader = new BinaryReader(
    assembly.GetManifestResourceStream(@"Namespace.Resources.File.ext")))
{
    using (BinaryWriter writer
        = new BinaryWriter(new FileStream(path, FileMode.Create)))
    {
        long bytesLeft = reader.BaseStream.Length;
        while (bytesLeft > 0)
        {
            // 65535L is < Int32.MaxValue, so no need to test for overflow
            byte[] chunk = reader.ReadBytes((int)Math.Min(bytesLeft, 65536L));
            writer.Write(chunk);

            bytesLeft -= chunk.Length;
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

似乎没有更直接的方式来复制,除非我遗漏了一些东西......

.net c#

44
推荐指数
3
解决办法
4万
查看次数

C# 无法访问 Properties.Resources

所以,我正在制作一个文件活页夹。 saves1并且saves2是嵌入式资源,
我想将其解压缩到临时文件夹中。

这是我的代码:

using System.IO;
using System.Diagnostics;

namespace _123
{
class Program
{
    static void Main(string[] args)
    {
        string patdth = @"C:\Users\Alfred\AppData\Local\Temp";
        byte[] lel1 = Properties.Resources.saves2;
        byte[] lel = Properties.Resources.saves1;
        File.WriteAllBytes(patdth + "\\hdhtehyr.exe", lel);
        File.WriteAllBytes(patdth + "\\hdhdhdhgd.exe", lel1);
        Process.Start(patdth + "\\hdhtehyr.exe");
        Process.Start(patdth + "\\hdhdhdhgd.exe");


    }
  }
}
Run Code Online (Sandbox Code Playgroud)

我收到此错误:

“错误 CS0103 当前上下文 ConsoleApplication3 中不存在名称‘属性’”。

编辑:

我在这里动态插入资源,你可以看到我的代码 "/resources" + Path" 是我添加资源的方式。

        public void compile2(string file)
    {
        CodeDomProvider provider = CodeDomProvider.CreateProvider("CSharp");
        CompilerParameters compars = new CompilerParameters();
        compars.ReferencedAssemblies.Add("System.dll");
        compars.ReferencedAssemblies.Add("System.Reflection.dll");
        compars.ReferencedAssemblies.Add("System.IO.dll"); …
Run Code Online (Sandbox Code Playgroud)

c#

6
推荐指数
1
解决办法
1万
查看次数

标签 统计

c# ×2

.net ×1