如何在c#programatically

时间:2017-06-23 18:52:07

标签: c# .net wcf dll

我正在尝试以编程方式从System32或SysWOW64目录中移动一个dll文件。我使用以下代码:

string path32 = "c:/windows/System32/";
string path64 = "c:/windows/SysWOW64/";
string fileName = "mydll.dll";
string[] paths = { path32, path64 };
foreach (string p in paths)
{
    if (int.Parse(year) == 1)
    {
        DirectoryInfo d = new DirectoryInfo(p);
        FileInfo[] infos = d.GetFiles(fileName);
        foreach (FileInfo f in infos)
        {
            // Do the renaming here
            File.Move(f.FullName, Path.Combine(f.DirectoryName, "1" + f.Name));
        }
    }
}

我在WCF服务中的某些条件下使用此方法。当我这样做我的当地环境,它工作正常。但是当我在不同的机器上运行时,它会抛出以下错误:

  

服务器遇到处理请求的错误。请看看   用于构造对服务的有效请求的服务帮助页面。该   异常消息是“拒绝访问路径。”#39;。查看服务器日志   更多细节。异常堆栈跟踪是:

     

at System.IO .__ Error.WinIOError(Int32 errorCode,String maybeFullPath)   在System.IO.File.InternalMove(String sourceFileName,String   destFileName,Boolean checkHost)   在SyncInvokeCalculateFestivaldate(Object,Object [],Object [])at   System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(对象   实例,对象[]输入,对象[]&输出)   System.ServiceModel.Dispatcher.DispatchOperationRuntime.InvokeBegin(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage5(MessageRpc&安培;   rpc)at   System.ServiceModel.Dispatcher.ImmutableDispatchRuntime.ProcessMessage31(MessageRpc&安培;   rpc)at System.ServiceModel.Dispatcher.MessageRpc.Process(Boolean   isOperationContextSet)

如何授予c#访问权限,以便重命名和移动dll文件?

1 个答案:

答案 0 :(得分:0)

要更改所需文件夹中的内容,您需要提升(管理员)Windows权限,即您的应用必须以管理员身份运行 - 这是一个很大的安全禁忌。您可能是本地管理员,因此有权更改您自己的系统,这就是它适用于您当地环境的原因。