How to check File/Directory Access for particular user and allow that user to read/Write into that directory.

(In order to Enlarge images Please Right Click and Open images in New Tab).



Let’s see How to check File/Directory Access for particular user and allow that user to read/Write into that directory.


In order to do this perform below steps



Add this class in your project with name NetworkConnection


public class NetworkConnection : IDisposable
string _networkName;

public NetworkConnection(string networkName,
NetworkCredential credentials)
_networkName = networkName;

var netResource = new NetResource()
Scope = ResourceScope.GlobalNetwork,
ResourceType = ResourceType.Disk,
DisplayType = ResourceDisplaytype.Share,
RemoteName = networkName

var result = WNetAddConnection2(

if (result != 0)
//throw new Win32Exception(result, “Error connecting to remote share”);


public void Dispose()

protected virtual void Dispose(bool disposing)
WNetCancelConnection2(_networkName, 0, true);

private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);

private static extern int WNetCancelConnection2(string name, int flags,
bool force);

Step-2) also add this class as well NetResource

public class NetResource
public ResourceScope Scope;
public ResourceType ResourceType;
public ResourceDisplaytype DisplayType;
public int Usage;
public string LocalName;
public string RemoteName;
public string Comment;
public string Provider;

public enum ResourceScope : int
Connected = 1,

public enum ResourceType : int
Any = 0,
Disk = 1,
Print = 2,
Reserved = 8,

public enum ResourceDisplaytype : int
Generic = 0x0,
Domain = 0x01,
Server = 0x02,
Share = 0x03,
File = 0x04,
Group = 0x05,
Network = 0x06,
Root = 0x07,
Shareadmin = 0x08,
Directory = 0x09,
Tree = 0x0a,
Ndscontainer = 0x0b



Let us see how to use it in order to check access for particular user with domain name and user  credentials:

NetworkCredential theNetworkCredential = new NetworkCredential(“username”, “password”, “domainName”);

//Here NetworkCredential  it’s from System.Net;

Now use it like

using (new NetworkConnection(FilePath , theNetworkCredential))

//your code goes here where you can perform operations on File
//FilePath should be with filename into it

byte[] fileData = Convert.FromBase64String(FileBinaryData);
Stream stream = new MemoryStream(fileData);

//Below code is for writing into file
using (new NetworkConnection(FilePath , theNetworkCredential))
File.WriteAllBytes(FilePath + item.FileName, fileData);

using (Stream file = File.Create(FilePath , fileData.Length, FileOptions.WriteThrough))


//Below code is for reading from file
byte[] fileBinary = null;
fileBinary = File.ReadAllBytes(FilePath);

//Use this fileBinary for rendering on UI




using ( new Impersonator( "YourUsername", "YourDomainname", "YourPassword" ) )
   //Here your all code goes which is going to perform I/O on
   // Files/Directories
//Which avoids all access path exceptions


add this class





Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google+ photo

You are commenting using your Google+ account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )


Connecting to %s