Network file copy in .NET

2020-03-26 08:21发布

问题:

I have an Ubuntu box running a Samba share open to everyone. I can access it via \ip address so I know I have full access to it.

From within my application I am trying the following but it will not work via the ip address only the DNS name.

// val = ip address
File.Copy("\\\\" + val + "\\share\\vSphere\\vSphere.exe", Temp + "vSphere.exe", true);

I need to use the IP Address as people who are VPN'ing in won't be able to have the program access the dns name only the ip address.

回答1:

First, try by giving IP address as below

File.Copy(@"\\192.100.1.23\share\vSphere\vSphere.exe", Path.combine(Temp ,"vSphere.exe"), true);

if error exist try using impersonate, give user name and password

AppDomain.CurrentDomain.SetPrincipalPolicy(PrincipalPolicy.WindowsPrincipal);

WindowsIdentity idnt = new WindowsIdentity(username, password);

WindowsImpersonationContext context = idnt.Impersonate();

File.Copy(@"\\192.100.1.23\share\vSphere\vSphere.exe", Path.combine(Temp ,"vSphere.exe"), true);

context.Undo();