Click here to Skip to main content
14,928,016 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:

Can anyone help me in accessing a shared path with username and password via C# web application code ?
The shared path is on a different server.
And also can somebody provide me with the rights the shared folder path should have ?

You should be able to get to network shares as long as the server box has a driver letter connected to them (the share has to be mapped on the web server box). Your only problem after that is whether or not you have sufficient permissions for the files/folders, so you may need to perform some impersonation.

Google is your friend.
Used the below piece of code to access shared path with username and password :

The below piece of code can be invoked from another class by using the simple syntax as stated below :

UNCAccess unc = new UNCAccess(ConfigurationSettings.AppSettings["SharedPath"].ToString(), ConfigurationSettings.AppSettings["SharedPathUser"].ToString(), ConfigurationSettings.AppSettings["SharedPathDomain"].ToString(), ConfigurationSettings.AppSettings["SharedPathPassword"].ToString());

using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Runtime.InteropServices;

/// <summary>
/// Summary description for UNCAccess
/// </summary>
public class UNCAccess
    [StructLayout(LayoutKind.Sequential, CharSet = CharSet.Unicode)]
    internal struct USE_INFO_2
        internal String ui2_local;
        internal String ui2_remote;
        internal String ui2_password;
        internal UInt32 ui2_status;
        internal UInt32 ui2_asg_type;
        internal UInt32 ui2_refcount;
        internal UInt32 ui2_usecount;
        internal String ui2_username;
        internal String ui2_domainname;

    [DllImport("NetApi32.dll", SetLastError = true, CharSet = CharSet.Unicode)]
    internal static extern UInt32 NetUseAdd(
        String UncServerName,
        UInt32 Level,
        ref USE_INFO_2 Buf,
        out UInt32 ParmError);

    private string sUNCPath;
    private string sUser;
    private string sPassword;
    private string sDomain;
    private int iLastError;
    public UNCAccess()
    public UNCAccess(string UNCPath, string User, string Domain, string Password)
        login(UNCPath, User, Domain, Password);
    public int LastError
        get { return iLastError; }

    /// <summary>
    /// Connects to a UNC share folder with credentials
    /// </summary>
    /// <param name="UNCPath">UNC share path</param>
    /// <param name="User">Username</param>
    /// <param name="Domain">Domain</param>
    /// <param name="Password">Password</param>
    /// <returns>True if login was successful</returns>
    public bool login(string UNCPath, string User, string Domain, string Password)
        sUNCPath = UNCPath;
        sUser = User;
        sPassword = Password;
        sDomain = Domain;
        return NetUseWithCredentials();
    private bool NetUseWithCredentials()
        uint returncode;
            USE_INFO_2 useinfo = new USE_INFO_2();

            useinfo.ui2_remote = sUNCPath;
            useinfo.ui2_username = sUser;
            useinfo.ui2_domainname = sDomain;
            useinfo.ui2_password = sPassword;
            useinfo.ui2_asg_type = 0;
            useinfo.ui2_usecount = 1;
            uint paramErrorIndex;
            returncode = NetUseAdd(null, 2, ref useinfo, out paramErrorIndex);
            iLastError = (int)returncode;
            return returncode == 0;
            iLastError = Marshal.GetLastWin32Error();
            return false;

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900