-2

I am trying to move one dll file programatically in c# from System32 or SysWOW64 directory. I am using following code:

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));
        }
    }
}

I am using this method on certain condition in WCF service. When I do it my local environment, it is working fine. But when I am running on different machine, it is throwing following error:

the server encountered an error processing the request. Please see the service help page for constructing valid requests to the service. The exception message is 'Access to the path is denied.'. See server logs for more details. The exception stack trace is:

at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.File.InternalMove(String sourceFileName, String destFileName, Boolean checkHost) at SyncInvokeCalculateFestivaldate(Object , Object[] , Object[] ) at System.ServiceModel.Dispatcher.SyncMethodInvoker.Invoke(Object instance, Object[] inputs, Object[]& outputs) at 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)

How can I give permission to get access in c# so that it will rename and move the dll file?

James Z
  • 12,209
  • 10
  • 24
  • 44
Kumar
  • 955
  • 5
  • 20
  • 50
  • Possible duplicate of https://stackoverflow.com/questions/2818179/how-do-i-force-my-net-application-to-run-as-administrator – Scott Chamberlain Jun 23 '17 at 18:58
  • Does the user the process is running as on the 2nd machine have permissions for the directory and files? – MORCHARD Jun 23 '17 at 18:59
  • @MORCHARD I guess, it is not. Then how to take permission programatically.? – Kumar Jun 24 '17 at 04:02
  • Negative vote commentor, may I know why this is getting negative vote. I know there are some similar questions but it is different than the other one, and I was not able to resolve the issue den only asked here. – Kumar Jun 24 '17 at 04:04
  • System32/SysWow64 are not part of *your* file system - they're the systems. There shouldn't be anything that *your* application wants to delete in there because it shouldn't have put anything in there in the first place. – Damien_The_Unbeliever Jul 21 '17 at 08:44

1 Answers1

0

To change stuff in the desired folders you need elevated (admin) windows privileges i.e. your app has to be run as an Administrator - which is a big security no-no. You are probably local admin and as such have rights to Change your own system that's why it works on your local Environment.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69