C# .NET - Accessing Shared folder on a Network Using C# Code

Asked By Sarfras S L P on 07-Sep-09 12:25 AM

Hi all,

   Can anyone help me to find a soution for Accessing Shared folder on a Network Using C# Code.

Thanking you in advance

Two ways - [)ia6l0 iii replied to Sarfras S L P on 07-Sep-09 12:49 AM

You can either use the DriveInfo class and its GetDrives method Or use the ManagementScope class.

Snippets below:

Snippet 1:
List<string> mappedDrives = new List<string>();
DriveInfo[] drives = DriveInfo.GetDrives();
foreach (DriveInfo drvInfo in drives)
{
      if (drvInfo.DriveType == DriveType.Network)
mappedDrives.Add(drvInfo.Name);
 }

Snippet 2:
ManagementScope mgmtScope = new ManagementScope();
//query for drivetype = 4..for  network drive
ObjectQuery objQuery = new ObjectQuery("SELECT VolumeName FROM Win32_LogicalDisk   WHERE DriveType= 4");
ManagementObjectSearcher mgmgObjSearch = new ManagementObjectSearcher(mgmtScope,   objQuery);
ManagementObjectCollection mgmgObjColl = mgmgObjSearch.Get();
foreach (ManagementObject mgmgObj in mgmgObjColl)
{
    mappedDrives.Add(mgmgObj["VolumeName"].ToString());
}
Vasanthakumar D replied to Sarfras S L P on 07-Sep-09 12:50 AM

Hi,

check here for details...

http://www.techtalkz.com/c-c-sharp/115668-accessing-unc-file-share-credentials.html

use DirectoryInfo and GetDirectories - Venkat K replied to Sarfras S L P on 07-Sep-09 12:54 AM

I have a program which searches our network. This runs off a fileshare path in the form of \\server\sharename

private bool CheckPath(string sPath)
{
DirectoryInfo objDir = new DirectoryInfo(sPath);

//Check whether path exists
if(objDir.Exists)
{
//find all directories
DirectoryInfo[] objSubDirs = objDir.GetDirectories();

//recurse each directory
for(int i = 0;i<objSubDirs.Length; i++)
{
CheckPath(objSubDirs[i].FullName);
}

//Do more stuff here
}

Thanks,

Accessing Shared folder on a Network Using C# Code - Sagar P replied to Sarfras S L P on 07-Sep-09 12:58 AM

private bool CheckPath(string sPath)
{
DirectoryInfo objDir = new DirectoryInfo(sPath);

//Check if its real!
if(objDir.Exists)
{
//find all directories
DirectoryInfo[] objSubDirs = objDir.GetDirectories();

//recurse each directory
for(int i = 0;i<objSubDirs.Length; i++)
{
CheckPath(objSubDirs[i].FullName);
}

//Do more stuff here
}

got thr this link as well;

http://www.codeproject.com/KB/dotnet/Browse_Network_Folders.aspx?display=PrintAll&fid=460887&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2813502

Re - Ravenet Rasaiyah replied to Sarfras S L P on 07-Sep-09 02:44 AM
Hi

You can access like this way also

http://www.codeproject.com/KB/dotnet/Browse_Network_Folders.aspx?display=PrintAll&fid=460887&df=90&mpp=25&noise=3&sort=Position&view=Quick&select=2813502

thank you
http://www.codegain.com
You can use the Win32 Net functions to get access - Trevor Dennis replied to Sarfras S L P on 13-Sep-09 03:54 AM
I found this solution to work for me when trying to access a file share on another computer.  It basically simulates doing a NET USE command where you can specify the credentials to use on the remote end.  The nice part is that you do not have to impersonate the user and it works across domains and workgroup computers.

First, declare these functions and types in your class.

using System.Reflection;
using System.Runtime.InteropServices;
using System.ComponentModel;

public class YourClass {
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2A(ref NetResource pstNetRes, string psPassword, string psUsername, int piFlags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2A(string psName, int piFlags, int pfForce);

[StructLayout(LayoutKind.Sequential)]
private struct NetResource {
    public int iScope;
    public int iType;
    public int iDisplayType;
    public int iUsage;
    public string sLocalName;
    public string sRemoteName;
    public string sComment;
    public string sProvider;
}

private const int RESOURCETYPE_DISK = 0x1;

Then, add these two methods.
   
    private void LoginToShare(string serverName, string shareName, string user, string password) {
        string destinationDirectory = string.Format(@"\\{0}\{1}", serverName, shareName);

        NetResource nr = new NetResource();
        nr.iScope = 2;
        nr.iType = RESOURCETYPE_DISK;
        nr.iDisplayType = 3;
        nr.iUsage = 1;
        nr.sRemoteName = destinationDirectory;
        nr.sLocalName = null;

        int flags = 0;
        int rc = WNetAddConnection2A(ref nr, password, user, flags);

        if (rc != 0) throw new Win32Exception(rc);
    }

    private void LogoutFromShare(string serverName, string shareName) {
        string destinationDirectory = string.Format(@"\\{0}\{1}", serverName, shareName);
        int flags = 0;
        int rc = WNetCancelConnection2A(destinationDirectory, flags, Convert.ToInt32(false));
    }


In your code, call the LoginToShare with the server, share, user, and password needed.  Then simply use the standard \\server\share\path conventions for accessing files on remote computers.  Call the LogoutFromShare when you're done.



wilson replied to Sarfras S L P on 24-Aug-11 11:37 AM
thanks, this code is very very useful, and solves a problem i have from a long time ago, thanks again