【发布时间】:2012-10-23 15:35:30
【问题描述】:
在下面的代码中,我试图从正在运行的应用程序中提取一个 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:\,但我总是收到此异常消息
怎么解决?
【问题讨论】:
-
您写信给
C:\Windows\System32,因为您明确使用了Environment.SystemDirectory;如果你写信给`D:`,我认为你不会看到这个问题。你跑高架了吗?请记住,在 Windows 中,“管理员用户”和“以管理员身份运行”是有区别的。