Hi
I want to know how can be get access to a shared folder on Server-side using VB6 or C#
I am able to connect to a shared folder on Server-side which is stored in C:\ but what if I want to access the shared folder which is in C:\Program Files\XXXX\SharedFolder
The code for mapping the shared folder in C:\ is as follows: -
There are two different classes.
1. Form.cs
<code>
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace MapDrive
{
public partial class frmMain : Form
{
public frmMain()
{
InitializeComponent();
}
private void btnMap_Click(object sender, EventArgs e)
{
NetworkDrive oNetDrive = new NetworkDrive();
ZUpdateStatus("Mapping Drive...");
try
{
oNetDrive.Force = this.conForce.Checked;
oNetDrive.LocalDrive = txtDrive.Text;
oNetDrive.ShareName = txtShareAddr.Text;
if (txtUser.Text == "" && txtPwd.Text == "")
oNetDrive.MapDrive();
else if (txtUser.Text == "")
oNetDrive.MapDrive(txtPwd.Text);
else
oNetDrive.MapDrive(txtUser.Text, txtPwd.Text);
ZUpdateStatus("Drive map successful...");
}
catch (Exception err)
{
ZUpdateStatus("Cannot map drive !!! : " + err.Message);
MessageBox.Show("Cannot map drive !!!\nError = " + err.Message);
}
oNetDrive = null;
}
private void ZUpdateStatus(string psStatus)
{
this.conStatusBar.Panels[0].Text = psStatus;
this.Refresh();
}
private void btnUnmap_Click(object sender, EventArgs e)
{
NetworkDrive oNetDrive = new NetworkDrive();
ZUpdateStatus("Unmapping drive...");
try
{
oNetDrive.Force = conForce.Checked;
oNetDrive.LocalDrive = txtDrive.Text;
oNetDrive.UnMapDrive();
ZUpdateStatus("Drive Unmap successful...");
}
catch (Exception err)
{
ZUpdateStatus("Cannot Unmap Drive !!! - " + err.Message);
MessageBox.Show("Cannot Unmap Drive !!!\nError = " + err.Message);
}
}
}
}
</code>
2. NetworkDrive.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace MapDrive
{
class NetworkDrive
{
[DllImport("mpr.dll")]
private static extern int WNetAddConnection2A(ref structNetResource pstNetRes, string psPassword, string psUsername, int piFlags);
[DllImport("mpr.dll")]
private static extern int WNetCancelConnection2A(string psName, int piFlags, int pfForce);
[DllImport("mpr.dll")]
private static extern int WNetConnectionDialog(int phWnd, int piType);
[DllImport("mpr.dll")]
private static extern int WNetDisconnectDialog(int phWnd, int piType);
[DllImport("mpr.dll")]
private static extern int WNetRestoreConnectionW(int phWnd, string psLocalDrive);
[StructLayout(LayoutKind.Sequential)]
private struct structNetResource
{
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;
private const int CONNECT_INTERACTIVE = 0x00000008;
private const int CONNECT_PROMPT = 0x00000010;
private const int CONNECT_UPDATE_PROFILE = 0x00000001;
private const int CONNECT_REDIRECT = 0x00000080;
private const int CONNECT_COMMANDLINE = 0x00000800;
private const int CONNECT_CMD_SAVERCD = 0x00001000;
private bool if_SaveCredentials = false;
public bool SaveCredentials
{
get { return (if_SaveCredentials); }
set {if_SaveCredentials=value;}
}
private bool if_Persistent = false;
public bool Persistent
{
get {return(if_Persistent);}
set { if_Persistent = value; }
}
private bool if_Force=false;
public bool Force
{
get{return (if_Force);}
set{if_Force=value;}
}
private bool ls_PromptForCredentials = false;
public bool PrompForCredentials
{
get { return (ls_PromptForCredentials); }
set { ls_PromptForCredentials = value; }
}
private string ls_Drive = "s:";
public string LocalDrive
{
get { return ls_Drive; }
set
{
if (value.Length >= 1)
{
ls_Drive = value.Substring(0, 1) + ":";
}
else
{
ls_Drive = "";
}
}
}
private string ls_ShareName = "\\\\Computer\\C$";
public string ShareName
{
get { return ls_ShareName; }
set { ls_ShareName = value; }
}
public void MapDrive() { zMapDrive(null, null); }
public void MapDrive(string Password) { zMapDrive(null,Password); }
public void MapDrive(string Username, string Password) { zMapDrive(Username, Password); }
public void UnMapDrive() { zUnMapDrive (this.if_Force); }
public void RestoreDrives() { zRestoreDrive(); }
public void ShowConnectDialog(Form ParentForm) { zDisplayDialog(ParentForm, 1); }
public void ShowDisconnecDialog(Form ParentForm) { zDisplayDialog(ParentForm, 2); }
private void zMapDrive(string psUsername, string psPassword)
{
structNetResource stNetRes = new structNetResource();
stNetRes.iScope = 2;
stNetRes.iType = RESOURCETYPE_DISK;
stNetRes.iDisplayType = 3;
stNetRes.iUsage = 1;
stNetRes.sRemoteName = ls_ShareName;
stNetRes.sLocalName = ls_Drive;
int iFlags = 0;
if (ls_PromptForCredentials) { iFlags += CONNECT_INTERACTIVE + CONNECT_PROMPT; }
if (psUsername == "") { psUsername = null; }
if (psPassword == "") { psPassword = null; }
int i = WNetAddConnection2A(ref stNetRes, psPassword, psUsername, iFlags);
if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
}
private void zUnMapDrive(bool pfForce)
{
int iFlags = 0;
int i = WNetCancelConnection2A(ls_Drive, iFlags, Convert.ToInt32(pfForce));
if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
}
private void zRestoreDrive()
{
int i = WNetRestoreConnectionW(0, null);
if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
}
private void zDisplayDialog(Form poParentForm, int piDialog)
{
int i = -1;
int iHandle = 0;
if (poParentForm != null)
{
iHandle = poParentForm.Handle.ToInt32();
}
if (piDialog == 1)
{
i = WNetConnectionDialog(iHandle, RESOURCETYPE_DISK);
}
else if (piDialog == 2)
{
i = WNetDisconnectDialog(iHandle, RESOURCETYPE_DISK);
}
if (i > 0) { throw new System.ComponentModel.Win32Exception(i); }
poParentForm.BringToFront();
}
}
}
With the above code, I am able to map a folder which is stored in C:\
But I want to map a folder which is stored may be in C:\Program Files\XXXXX\SharedFolder
Please help me with this.
--
AJ