To copy files from one location to shared location

shagil

New member
Joined
Jul 19, 2015
Messages
3
Programming Experience
5-10
Hi All,
I have a C# program to copy the files from one location to another and its working fine if the destination location is in same server but my requirement is to copy the files from server-1 to server-2 where i have a shared location created (\\Server2\Testfile). From Server1 i have the permission to access the shared folder, copy the files, delete files but through program its says "Access to the path '\\Server2\Testfile' is denied.


Thanks

Shagil
 

shagil

New member
Joined
Jul 19, 2015
Messages
3
Programming Experience
5-10
This is the part of coding where i do the file copying, i tried without impersonation but same result


[DllImport("advapi32.dll", SetLastError = true)]
public static extern bool LogonUser(string pszUsername, string pszDomain, string pszPassword,
int dwLogonType, int dwLogonProvider, ref IntPtr phToken);

// closes open handes returned by LogonUser
[DllImport("kernel32.dll", CharSet = CharSet.Auto)]
public extern static bool CloseHandle(IntPtr handle);

//elevate privileges before doing file copy to handle domain security
WindowsImpersonationContext impersonationContext = null;
IntPtr userHandle = IntPtr.Zero;
const int LOGON32_PROVIDER_DEFAULT = 0;
const int LOGON32_LOGON_INTERACTIVE = 2;
string domain = ConfigurationManager.AppSettings["ImpersonationDomain"];
string user = ConfigurationManager.AppSettings["ImpersonationUserName"];
string password = ConfigurationManager.AppSettings["ImpersonationPassword"];

// if domain name was blank, assume local machine
if (domain == "")
{
domain = System.Environment.MachineName;
}

// Call LogonUser to get a token for the user
bool loggedOn = LogonUser(user,domain,password,LOGON32_LOGON_INTERACTIVE,LOGON32_PROVIDER_DEFAULT,ref userHandle);

if (!loggedOn)
{
WriteErrorLog("Exception impersonating user, error code: " + Marshal.GetLastWin32Error());

}
// Begin impersonating the user
impersonationContext = WindowsIdentity.Impersonate(userHandle);

//run the program with elevated privileges (like file copying from a domain server)
// Here i do the file copiying
foreach (string s in files)
{

System.IO.File.Copy(s, System.Configuration.ConfigurationManager.AppSettings["DestinatioinFilePath"].ToString() + FileName, true);
}
impersonationContext.Undo();
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,271
Location
Sydney, Australia
Programming Experience
10+
You have made your code very hard to read. In future, please use actual code formatting tags, i.e.

[xcode=c#]your code here[/xcode]

to get syntax highlighting or:

[code]your code here[/code]

if you want to add extra formatting, e.g. bold.
 

shagil

New member
Joined
Jul 19, 2015
Messages
3
Programming Experience
5-10
Hi jmcilhinney


Can you suggest any of the coding for the below scenario ?

"I have a C# program to copy the files from one location to another and its working fine if the destination location is in same server but my requirement is to copy the files from server-1 to server-2 where i have a shared location created (\\Server2\Testfile). From Server1 i have the permission to access the shared folder, copy the files, delete files but through program its says "Access to the path '\\Server2\Testfile' is denied."


 
Top Bottom