473,386 Members | 1,710 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

copy file to authenticated folder on server

I am trying to copy the file from C:\Temp folder to one of folder on server which needs to have authenticated. (folder has username and password) my code is below:

Expand|Select|Wrap|Line Numbers
  1. NetworkShare.DisconnectFromShare(@"\\server-a\DBFiles", true); //Disconnect in case we are currently connected with our credentials;
  2. NetworkShare.ConnectToShare(@"\\server-a\DBFiles", "user1", "password1!"); //Connect with the new credentials
  3. File.Copy(@"c:\temp\T1.txt", @"\\server-a\DBFiles\T1.txt");
  4. NetworkShare.DisconnectFromShare(@"\\server-a\DBFiles", false); //Disconnect from the server.
  5.  
  6. and then Networkshare static class: 
  7.  
  8. public static class NetworkShare
  9.         {
  10.             /// <summary>
  11.             /// Connects to the remote share
  12.             /// </summary>
  13.             /// <returns>Null if successful, otherwise error message.</returns>
  14.             public static string ConnectToShare(string uri, string username, string password)
  15.             {
  16.                 //Create netresource and point it at the share
  17.                 NETRESOURCE nr = new NETRESOURCE();
  18.                 nr.dwType = RESOURCETYPE_DISK;
  19.                 nr.lpRemoteName = uri;
  20.  
  21.                 //Create the share
  22.                 int ret = WNetUseConnection(IntPtr.Zero, nr, password, username, 0, null, null, null);
  23.  
  24.                 //Check for errors
  25.                 if (ret == NO_ERROR)
  26.                     return null;
  27.                 else
  28.                     return GetError(ret);
  29.             }
  30.  
  31.             /// <summary>
  32.             /// Remove the share from cache.
  33.             /// </summary>
  34.             /// <returns>Null if successful, otherwise error message.</returns>
  35.             public static string DisconnectFromShare(string uri, bool force)
  36.             {
  37.                 //remove the share
  38.                 int ret = WNetCancelConnection(uri, force);
  39.  
  40.                 //Check for errors
  41.                 if (ret == NO_ERROR)
  42.                     return null;
  43.                 else
  44.                     return GetError(ret);
  45.             }
  46.  
  47.             #region P/Invoke Stuff
  48.             [DllImport("Mpr.dll")]
  49.             private static extern int WNetUseConnection(
  50.                 IntPtr hwndOwner,
  51.                 NETRESOURCE lpNetResource,
  52.                 string lpPassword,
  53.                 string lpUserID,
  54.                 int dwFlags,
  55.                 string lpAccessName,
  56.                 string lpBufferSize,
  57.                 string lpResult
  58.                 );
  59.  
  60.             [DllImport("Mpr.dll")]
  61.             private static extern int WNetCancelConnection(
  62.                 string lpName,
  63.                 bool fForce
  64.                 );
  65.  
  66.             [StructLayout(LayoutKind.Sequential)]
  67.             private class NETRESOURCE
  68.             {
  69.                 public int dwScope = 0;
  70.                 public int dwType = 0;
  71.                 public int dwDisplayType = 0;
  72.                 public int dwUsage = 0;
  73.                 public string lpLocalName = "";
  74.                 public string lpRemoteName = "";
  75.                 public string lpComment = "";
  76.                 public string lpProvider = "";
  77.             }
  78.  
  79.             #region Consts
  80.             const int RESOURCETYPE_DISK = 0x00000001;
  81.             const int CONNECT_UPDATE_PROFILE = 0x00000001;
  82.             #endregion
  83.  
  84.             #region Errors
  85.             const int NO_ERROR = 0;
  86.  
  87.             const int ERROR_ACCESS_DENIED = 5;
  88.             const int ERROR_ALREADY_ASSIGNED = 85;
  89.             const int ERROR_BAD_DEVICE = 1200;
  90.             const int ERROR_BAD_NET_NAME = 67;
  91.             const int ERROR_BAD_PROVIDER = 1204;
  92.             const int ERROR_CANCELLED = 1223;
  93.             const int ERROR_EXTENDED_ERROR = 1208;
  94.             const int ERROR_INVALID_ADDRESS = 487;
  95.             const int ERROR_INVALID_PARAMETER = 87;
  96.             const int ERROR_INVALID_PASSWORD = 1216;
  97.             const int ERROR_MORE_DATA = 234;
  98.             const int ERROR_NO_MORE_ITEMS = 259;
  99.             const int ERROR_NO_NET_OR_BAD_PATH = 1203;
  100.             const int ERROR_NO_NETWORK = 1222;
  101.             const int ERROR_SESSION_CREDENTIAL_CONFLICT = 1219;
  102.  
  103.             const int ERROR_BAD_PROFILE = 1206;
  104.             const int ERROR_CANNOT_OPEN_PROFILE = 1205;
  105.             const int ERROR_DEVICE_IN_USE = 2404;
  106.             const int ERROR_NOT_CONNECTED = 2250;
  107.             const int ERROR_OPEN_FILES = 2401;
  108.  
  109.             private struct ErrorClass
  110.             {
  111.                 public int num;
  112.                 public string message;
  113.                 public ErrorClass(int num, string message)
  114.                 {
  115.                     this.num = num;
  116.                     this.message = message;
  117.                 }
  118.             }
  119.  
  120.             private static ErrorClass[] ERROR_LIST = new ErrorClass[] {
  121.         new ErrorClass(ERROR_ACCESS_DENIED, "Error: Access Denied"), 
  122.         new ErrorClass(ERROR_ALREADY_ASSIGNED, "Error: Already Assigned"), 
  123.         new ErrorClass(ERROR_BAD_DEVICE, "Error: Bad Device"), 
  124.         new ErrorClass(ERROR_BAD_NET_NAME, "Error: Bad Net Name"), 
  125.         new ErrorClass(ERROR_BAD_PROVIDER, "Error: Bad Provider"), 
  126.         new ErrorClass(ERROR_CANCELLED, "Error: Cancelled"), 
  127.         new ErrorClass(ERROR_EXTENDED_ERROR, "Error: Extended Error"), 
  128.         new ErrorClass(ERROR_INVALID_ADDRESS, "Error: Invalid Address"), 
  129.         new ErrorClass(ERROR_INVALID_PARAMETER, "Error: Invalid Parameter"), 
  130.         new ErrorClass(ERROR_INVALID_PASSWORD, "Error: Invalid Password"), 
  131.         new ErrorClass(ERROR_MORE_DATA, "Error: More Data"), 
  132.         new ErrorClass(ERROR_NO_MORE_ITEMS, "Error: No More Items"), 
  133.         new ErrorClass(ERROR_NO_NET_OR_BAD_PATH, "Error: No Net Or Bad Path"), 
  134.         new ErrorClass(ERROR_NO_NETWORK, "Error: No Network"), 
  135.         new ErrorClass(ERROR_BAD_PROFILE, "Error: Bad Profile"), 
  136.         new ErrorClass(ERROR_CANNOT_OPEN_PROFILE, "Error: Cannot Open Profile"), 
  137.         new ErrorClass(ERROR_DEVICE_IN_USE, "Error: Device In Use"), 
  138.         new ErrorClass(ERROR_EXTENDED_ERROR, "Error: Extended Error"), 
  139.         new ErrorClass(ERROR_NOT_CONNECTED, "Error: Not Connected"), 
  140.         new ErrorClass(ERROR_OPEN_FILES, "Error: Open Files"), 
  141.         new ErrorClass(ERROR_SESSION_CREDENTIAL_CONFLICT, "Error: Credential Conflict"),
  142.     };
  143.  
  144.             private static string GetError(int errNum)
  145.             {
  146.                 foreach (ErrorClass er in ERROR_LIST)
  147.                 {
  148.                     if (er.num == errNum) 
  149.                         return er.message;
  150.                     Console.WriteLine(er.num);
  151.                     Console.WriteLine(errNum);
  152.                 }
  153.                 return "Error: Unknown, " + errNum;
  154.             }
  155.             #endregion
  156.  
  157.             #endregion
  158.         }
  159.  
  160. Error I am getting on 
  161. foreach (ErrorClass er in ERROR_LIST)
  162.                 {
  163.                     if (er.num == errNum) 
  164.                         return er.message;
  165.                     Console.WriteLine(er.num);
  166.                     Console.WriteLine(errNum);
  167.                 }
Error is :
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!
Jul 24 '13 #1
0 3289

Sign in to post your reply or Sign up for a free account.

Similar topics

5
by: hntgzr | last post by:
I am trying to include a function in a .php file on a different server from the main .php files. I am using: include_path=http://www.anotherserver.com/foldername;...
1
by: Thomasa Gregg | last post by:
I am trying to find a way to allow the user to select a folder on the client machine and then use that information to move a file from the server into that folder. I am not sure if this is the...
0
by: Grey | last post by:
I have a ASP.NET web application which is required to copy file from windows server to Novell Netware server. I have used impersonation to connect to Netware server. The application works fine when...
9
by: CGW | last post by:
I asked the question yesterday, but know better how to ask it, today: I'm trying to use the File.Copy method to copy a file from a client to server (.Net web app under IIS ). It looks to me that...
2
by: LiveCycle | last post by:
Hi, I'm writing a web service, and, as part of its function, I want it to copy a file from a remote server and place that file on the local web server under another name. However, I always get...
3
by: phil25bg | last post by:
Hello , it's about the program in c# , one of it's functions is to copy file(s) from one folder to another in time chosen by the user. What is the proper way to keep aplication a life chaking...
2
by: deepaks85 | last post by:
Hello everyone, I am trying to copy a file with filesystemobject but I don't know where I am doing mistake. Please help me out.... Here is the code............. file.htm
1
by: =?Utf-8?B?UHJpeWE=?= | last post by:
Hi , I m usin ga webservice to copy a file to a remote server. When i do that i m getting ' Access to path denied' error. Pls let me know what permission i need to give. I tried giving...
2
by: CGatto | last post by:
Hi, We have just started getting the following error during compiles of our forms-based application. We are developing in VS2008, VB.Net, with Team Foundation Server-based source control. ...
5
by: =?Utf-8?B?QWRyaWFuTW9ycmlz?= | last post by:
Hello! I'm trying to copy a file from another computer on the network that I do not have permission with my current logon details to access. If I open the folder using the Windows file manager...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.