在下面的代码中,我试图从正在运行的应用程序中提取一个 dll 文件并将其保存到 System32 目录:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Diagnostics;
using Microsoft.VisualBasic;
using System.Security.Cryptography;
using System.Runtime.InteropServices;
using System.Reflection;
using System.Windows.Forms;
namespace ConsoleApplication1
{
class Program
{
public static void ExtractSaveResource(String filename, String location)
{
// Assembly assembly = Assembly.GetExecutingAssembly();
Assembly a = Assembly.GetExecutingAssembly();
// Stream stream = assembly.GetManifestResourceStream("Installer.Properties.mydll.dll"); // or whatever
// string my_namespace = a.GetName().Name.ToString();
Stream resFilestream = a.GetManifestResourceStream(filename);
if (resFilestream != null)
{
try
{
BinaryReader br = new BinaryReader(resFilestream);
FileStream fs = new FileStream(location, FileMode.Create); // say
BinaryWriter bw = new BinaryWriter(fs);
byte[] ba = new byte[resFilestream.Length];
resFilestream.Read(ba, 0, ba.Length);
bw.Write(ba);
br.Close();
bw.Close();
resFilestream.Close();
}
catch (Exception E) { MessageBox.Show(E.Message); }
}
// this.Close();
}
static void Main(string[] args)
{
string systemDir = Environment.SystemDirectory;
ExtractSaveResource("MySql.Data.dll",systemDir);
}
}
}
异常消息:
Access to the path C:\Windows\System32 is denied
我试图将文件复制到其他目录,如 D:\ 或 X:\ 但我总是收到此异常消息
如何解决?