copy file to authenticated folder on server in c#


i trying copy file c:\temp folder 1 of folder on server needs have authenticated. (folder has username , password) code below: 

networkshare.disconnectfromshare(@"\\server-a\dbfiles", true); //disconnect in case connected our credentials;
networkshare.connecttoshare(@"\\server-a\dbfiles", "user1", "password1!"); //connect new credentials
file.copy(@"c:\temp\t1.txt", @"\\server-a\dbfiles\t1.txt");
networkshare.disconnectfromshare(@"\\server-a\dbfiles", false); //disconnect server.

and networkshare static class: 

public static class networkshare
{
/// <summary>
/// connects remote share
/// </summary>
/// <returns>null if successful, otherwise error message.</returns>
public static string connecttoshare(string uri, string username, string password)
{
//create netresource , point @ share
netresource nr = new netresource();
nr.dwtype = resourcetype_disk;
nr.lpremotename = uri;

//create share
int ret = wnetuseconnection(intptr.zero, nr, password, username, 0, null, null, null);

//check errors
if (ret == no_error)
return null;
else
return geterror(ret);
}

/// <summary>
/// remove share cache.
/// </summary>
/// <returns>null if successful, otherwise error message.</returns>
public static string disconnectfromshare(string uri, bool force)
{
//remove share
int ret = wnetcancelconnection(uri, force);

//check errors
if (ret == no_error)
return null;
else
return geterror(ret);
}

#region p/invoke stuff
[dllimport("mpr.dll")]
private static extern int wnetuseconnection(
intptr hwndowner,
netresource lpnetresource,
string lppassword,
string lpuserid,
int dwflags,
string lpaccessname,
string lpbuffersize,
string lpresult
);

[dllimport("mpr.dll")]
private static extern int wnetcancelconnection(
string lpname,
bool fforce
);

[structlayout(layoutkind.sequential)]
private class netresource
{
public int dwscope = 0;
public int dwtype = 0;
public int dwdisplaytype = 0;
public int dwusage = 0;
public string lplocalname = "";
public string lpremotename = "";
public string lpcomment = "";
public string lpprovider = "";
}

#region consts
const int resourcetype_disk = 0x00000001;
const int connect_update_profile = 0x00000001;
#endregion

#region errors
const int no_error = 0;

const int error_access_denied = 5;
const int error_already_assigned = 85;
const int error_bad_device = 1200;
const int error_bad_net_name = 67;
const int error_bad_provider = 1204;
const int error_cancelled = 1223;
const int error_extended_error = 1208;
const int error_invalid_address = 487;
const int error_invalid_parameter = 87;
const int error_invalid_password = 1216;
const int error_more_data = 234;
const int error_no_more_items = 259;
const int error_no_net_or_bad_path = 1203;
const int error_no_network = 1222;
const int error_session_credential_conflict = 1219;

const int error_bad_profile = 1206;
const int error_cannot_open_profile = 1205;
const int error_device_in_use = 2404;
const int error_not_connected = 2250;
const int error_open_files = 2401;

private struct errorclass
{
public int num;
public string message;
public errorclass(int num, string message)
{
this.num = num;
this.message = message;
}
}

private static errorclass[] error_list = new errorclass[] {
new errorclass(error_access_denied, "error: access denied"), 
new errorclass(error_already_assigned, "error: assigned"), 
new errorclass(error_bad_device, "error: bad device"), 
new errorclass(error_bad_net_name, "error: bad net name"), 
new errorclass(error_bad_provider, "error: bad provider"), 
new errorclass(error_cancelled, "error: cancelled"), 
new errorclass(error_extended_error, "error: extended error"), 
new errorclass(error_invalid_address, "error: invalid address"), 
new errorclass(error_invalid_parameter, "error: invalid parameter"), 
new errorclass(error_invalid_password, "error: invalid password"), 
new errorclass(error_more_data, "error: more data"), 
new errorclass(error_no_more_items, "error: no more items"), 
new errorclass(error_no_net_or_bad_path, "error: no net or bad path"), 
new errorclass(error_no_network, "error: no network"), 
new errorclass(error_bad_profile, "error: bad profile"), 
new errorclass(error_cannot_open_profile, "error: cannot open profile"), 
new errorclass(error_device_in_use, "error: device in use"), 
new errorclass(error_extended_error, "error: extended error"), 
new errorclass(error_not_connected, "error: not connected"), 
new errorclass(error_open_files, "error: open files"), 
new errorclass(error_session_credential_conflict, "error: credential conflict"),
};

private static string geterror(int errnum)
{
foreach (errorclass er in error_list)
{
if (er.num == errnum) 
return er.message;
console.writeline(er.num);
console.writeline(errnum);
}
return "error: unknown, " + errnum;
}
#endregion

#endregion
}

error getting on 
foreach (errorclass er in error_list)
{
if (er.num == errnum) 
return er.message;
console.writeline(er.num);
console.writeline(errnum);
}
error :
5 2250 85 2250 1200 2250 67 2250 1204 2250 1223 2250 1208 2250 487 2250 87 2250 1216 2250 234 2250 259 2250 1203 2250 1222 2250 1206
2250 1205 2250 2404 2250 1208 2250 

5 53 85 53 1200 53 67 53 1204 53 1223 53 1208 53 487 53 87 53 1216 53 234 53 259 53 1203 53 1222 53 1206 53 1205 53 2404 53 1208 53
2250 53 2401 53 1219 53


a first chance exception of type 'system.unauthorizedaccessexception' occurred in mscorlib.dll

please help!

hi ms_sawan,

if (er.num == errnum)      return er.message; console.writeline(er.num); console.writeline(errnum);

the output numbers not error messages. console always  write numbers according above code.

wnetcancelconnection() returns 2250("error: not connected") when call networkshare.disconnectfromshare() method.

wnetuseconnection() returns 53("should error: unknown, 53") when call networkshare.connecttoshare() method.

error seems come here. when connect shared folder, fails, still  try copy file folder. under cercomstance, not have permission access shared folder.

so suggest make sure username , password correct. if can't connect shared folder, catch exceptions, not try copy files when username or password not correct.


caillen
msdn community support | feedback us
develop , promote apps in windows store
please remember mark replies answers if , unmark them if provide no help.



Visual Studio Languages  ,  .NET Framework  >  Visual C#



Comments

Popular posts from this blog

Azure DocumentDB Owner resource does not exist

job syspolicy_purge_history job fail in sqlserver 2008

Trying to register with public marketplace error with 'Get-AzureStackStampInformation'