Moving a file from a directory to a network share programmatically

强颜欢笑 提交于 2019-12-12 03:32:13

问题


I am attempting to write a process that will move a file from a directory on a server, to a network share, that will not be on that computer. The methodology worked just fine when moving from my local machine to the network share. However, it is not working when I put the process on my server (this process is basically a dll, that gets picked up by a running service, the service spins up a thread, gives it to my process, and away it goes). The process is running using an identity that has access to the shared directory, but it just won't move it. I can't detect an exception occuring either. I have pretty good logging in there and no exceptions seem to be thrown. I will look closer, but has anyone tried to do anything like this before? I call either this...

File.Move(@"C:\Output\" + FileTitle, ShareDirectory + "\\" + FileTitle);

Or this...

 File.Copy(@"C:\Output\" + FileTitle, ShareDirectory + "\\" + FileTitle, true);

depending on whether or not the file exists or not... I am not certain impersonating anything would solve anything? The service already runs under an ldap username that has access to the share...

UPDATE: More information discovered

Ok, I had an incorrect condition that was never hit, so that is why I wasn't catching an exception... I get this exception...

Exception Publishing Document to Share: System.UnauthorizedAccessException: Access to the path is denied.     

Once I saw that exception I decided to impersonate a valid user... And I am still getting that exception... Would mapping that directory on the server help at all? I am guessing no, but I am running out of ideas.

UPDATE: when I impersonate myself vs other users

When I impersonate my self in the process I am able to successfully move the file to the share. But If I impersonate another username that should have the same rights, I still get the above exception. I am not certain why this is occurring. Maybe this user doesn't have the permissions that I suspect it has. But just looking in windows and looking at the LDAP server it appears as though it does. Very curious indeed.

来源:https://stackoverflow.com/questions/14628412/moving-a-file-from-a-directory-to-a-network-share-programmatically

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!