using IWshRuntimeLibrary in C#

Hi Experts,

I would like to know how to use the using WshNetworkClass.....to safely Add Printers and Map a Network Drive.

I have managed to Add a printer passing it "" values for driver & port, although when I try to MapNetworkDrive I don't know what I should be supplying the ref objects?

Can anyone help me with this please...

Thanks in advance.
LVL 10
JoesmailAsked:
Who is Participating?
 
gregoryyoungCommented:
In poking around I found some sample code ...

public static void createmappeddrive(string driveletter, string
username, string pass)
{
bool blnUpdateProfile = false;
bool blnForce = true;
object objblnForce = (object)blnForce;
object objblnUpdateProfile = (object)blnUpdateProfile;
object objstrUserName;
object objstrPassword;
string strMapDriveLetter = driveletter + ":";
WshNetworkClass wshNET = new WshNetworkClass();
//"cbcg\\rwma"
objstrUserName = (object)username;
objstrPassword = (object)pass;
wshNET.MapNetworkDrive(strMapDriveLetter, @"\\networklocation\", ref
objblnUpdateProfile,ref objstrUserName, ref objstrPassword);
}


public static void removemapdrive(string driveletter)
{
bool blnUpdateProfile = false;
bool blnForce = true;
WshNetworkClass wshNET = new WshNetworkClass();
object objblnForce = (object)blnForce;
object objblnUpdateProfile = (object)blnUpdateProfile;
string strMapDriveLetter = driveletter + ":";
wshNET.RemoveNetworkDrive(strMapDriveLetter,ref objblnForce,ref
objblnUpdateProfile);
}
0
 
jagadeesh_motamarriCommented:
0
 
JoesmailAuthor Commented:
To be clearer....I am trying to map a drive using the following library....

using IWshRuntimeLibrary;
...
...
WshNetworkClass network = new  WshNetworkClass();
...
...
network.MapNetworkDrive(_drive,  @"\\" + _server + @"\" + _share,REF OBJECT, REF OBJECT, REF OBJECT);

Can anyone give me some examples of the REFs required here........
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
jagadeesh_motamarriCommented:
i guess this works in VBScript. It should successfully map to a network drive
\\server1\data.


Dim WshNetwork
Set WshNetwork = WScript.CreateObject("WScript.Network")
sPwd = inputbox("Enter password")
WshNetwork.MapNetworkDrive "J:", "\\server1\data", false,
"xyz_net\John", sPwd
Msgbox "Drives has been mapped successfully"
0
 
JoesmailAuthor Commented:
you guessed wrong.
0
 
JoesmailAuthor Commented:
Thanks for this gregoryyoung

Just one last question.  Do you know if there is a way around having to PASS the password.  I can identify the username easily enought Environment.Username although I don't know how I would pass the "password" credentials through?
0
 
gregoryyoungCommented:
I would imagine that is if you want to use a different username/pass (without knowing the library). My guess would be that passing null attempts to use your current token (but this is just a guess)
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.