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).

 

2

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

 

Step-1)

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(
netResource,
credentials.Password,
credentials.UserName,
0);

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

~NetworkConnection()
{
Dispose(false);
}

public void Dispose()
{
Dispose(true);
GC.SuppressFinalize(this);
}

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

[DllImport(“mpr.dll”)]
private static extern int WNetAddConnection2(NetResource netResource,
string password, string username, int flags);

[DllImport(“mpr.dll”)]
private static extern int WNetCancelConnection2(string name, int flags,
bool force);
}

Step-2) also add this class as well NetResource

[StructLayout(LayoutKind.Sequential)]
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,
GlobalNetwork,
Remembered,
Recent,
Context
};

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
}

 

Step-3)

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);
}
//OR

using (Stream file = File.Create(FilePath , fileData.Length, FileOptions.WriteThrough))
{
stream.CopyTo(file);

}

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

//Use this fileBinary for rendering on UI

}

 

                                          OR

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

 

refer:

https://www.codeproject.com/Articles/10090/A-small-C-Class-for-impersonating-a-User

 

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s