|
Hi..
FOL thanks for posting this code...its really useful...
I am facing problem. I have included this class in my windows service project.On startup I am mapping a shared location on the machine. It works differently on server operation system. I am not able to map drive in my service in Windows 2000 server.
How different it is on Windows 2000 server ??
regards,
Samir
|
|
|
|
|
I have not had much feedback about Windows 2000, it should have no issues... Maybe Windows is not allowing you to map a drive with the service because its using the SYSTEM account when executed as a service...
----------------------------
aejw.com
|
|
|
|
|
I am having the same issue too. The project can establish the mapping on Windows Server 2003, but not on 2000. Both servers are using Active Directory ( don't know if that has an effect ). Does anyone have any idea?
Best Regards,
ron
|
|
|
|
|
is there a way to use oNetDrive.ShareName to get a list of the drives and their paths?
|
|
|
|
|
I use WMI to get a list of shares from a given
machine. Although you can always use a wrapper
for Shell.Application if you don't have local admin.
You can also use WMI to get a list of drives.
"Select size,freespace from Win32_LogicalDisk where name = '" + DriveL + "'
When you do the share query you need to make sure you don't pull in Administrative shares.. Here is the WMI query for that..
"Select name,path from Win32_Share where type != '2147483648' and type != '2147483651'"
Please, keep in mind that if you use the Shell.Application method, that you do not have the ability to see any hidden or administrative shares. With WMI you can see them, if you need to.
|
|
|
|
|
Is it oke to use this code.
As I read in the license and i need your permissions i see.
But i cann't mail you?.
All i want to do is map 4 drives on a custommer who doesn't allow net use or vbscript.
jipijie i'm on VB
jipijee i'm on C
|
|
|
|
|
Hello and sorry about the very slow reply, yes it can be used... An updated version is on my site www.aejw.com - which has an updated and very lax EULA. I will update code project before long...
----------------------------
aejw.com
|
|
|
|
|
help.
i use Asp.net to implement map network drive, but can't complete.
Arne
-- modified at 23:41 Tuesday 13th December, 2005
|
|
|
|
|
Hi,
what difference you expect here by using p/ Invoke when..
Map Drives are any way session bound
Map drives are unstable/ disconnect as session ends
Map dirves connection depends on various other factors like net work connectivity etc.
So if any of these problems cannot be solved with p/Invoke, why don't you just use the dos command to map the network drive via your application and aslo to delete it.
Simply writting a simple class to simulate DOS prompt will do this, doesn't it?
L.W.C. Nirosh,
Tech Lead,
TextCENTRIC Technology,
Colombo,
Sri Lanka
|
|
|
|
|
wa??? I have no idea what your on about, but never mind... Try getting feedback / error messages from a command prompt, lol...
----------------------------
aejw.com
|
|
|
|
|
Sorry for the late reply..
But mate do you wanna code to do it??
Just say yes.. I will pass it.
L.W.C. Nirosh,
Colombo,
Sri Lanka.
|
|
|
|
|
if the requirement is just to be able to connect to an untrusted domain, say to work with files then disconnect, this code might suffice: (no need for a drive letter, if you know the share name)
public bool InvokeComMember(string progID, string member, System.Reflection.BindingFlags invokeAttr, params object[] param)
{
try
{
Type type = Type.GetTypeFromProgID(progID);
object o = Activator.CreateInstance(type);
type.InvokeMember(member, invokeAttr, null, o, param);
}
catch{}
finally
{
type = null;
o = null;
}
}
InvokeComMember("WScript.Network", "MapNetworkDrive", System.Reflection.BindingFlags.InvokeMethod, "", remoteName, false, user, password);
InvokeComMember("WScript.Network", "RemoveNetworkDrive", System.Reflection.BindingFlags.InvokeMethod, remoteName);
|
|
|
|
|
Beautiful. Exactly what I needed. Thank you so much!
|
|
|
|
|
Thanks for a great this great article. One thing that I desperatly needed was the inclusion of a function to automatically find an open drive letter for a faceless application. Very simple, so expand at your own leasure.
//Under the Function mapping region, add the following...
Public bool GetNextOpenDriveLetter(ref string Driveletter) {return zGetNextOpenDriveLetter(ref Driveletter);}
//Under the Core functions region, add the following...
private bool CheckDriveLetter(string DriveLetter) {
bool retVal = false;
DriveLetter += @":\";
string[] drives = System.IO.Directory.GetLogicalDrives();
foreach (string str in drives) {
if (str.ToString() == DriveLetter.ToString()) {
retVal = true;
break;
}
}
return retVal;
}
private bool zGetNextOpenDriveLetter(ref string Driveletter) {
string[] openletters = {"L", "M", "N"}; //ect.
bool retVal = false;
//See if letter given is open
if (CheckDriveLetter(Driveletter)) {
foreach (string str in openletters) {
if (!CheckDriveLetter(str.ToString())) {
Driveletter = str;
retVal = true;
break;
}
}
}
return retVal;
}
//Example...
NetworkDrive drive = new NetworkDrive();
string driveletter = "L";
if (drive.GetNextOpenDriveLetter(ref driveletter)) {
drive.ShareName = @"\\server\share";
drive.LocalDrive = driveletter;
drive.Force = true;
drive.MapDrive();
}
Enjoy!
It needs more cowbell!
|
|
|
|
|
Good plan, ill sort out an update for build 0016, what name would like for credit?
----------------------------
Adam Woods
|
|
|
|
|
Wow, never thought anyone would even care...
Thanks Adam, you may credit me as Hannes "the zipper" Burger.
|
|
|
|
|
I also needed a way to find the next open drive. Here is what I came up with. Also included is a function to list current usage of network drives.
Private Const NO_ERROR As Int32 = 0
Private Const ERROR_NOT_CONNECTED As Int32 = 2250I
'Drive Types
Private Const DRIVE_UNKNOWN As Int32 = 0
Private Const DRIVE_NO_ROOT_DIR As Int32 = 1
Private Const DRIVE_REMOVABLE As Int32 = 2
Private Const DRIVE_FIXED As Int32 = 3
Private Const DRIVE_REMOTE As Int32 = 4
Private Const DRIVE_CDROM As Int32 = 5
Private Const DRIVE_RAMDISK As Int32 = 6
<dllimport("kernel32.dll", setlasterror:="True)"> _
Private Shared Function GetDriveType(ByVal strDrive As String) As Int32
End Function
<dllimport("mpr.dll", setlasterror:="True)"> _
Private Shared Function WNetGetConnection(ByVal strLocalName As String, _
ByVal sbRemoteName As StringBuilder, ByRef intLength As Int32) As Int32
End Function
Public Function GetNextAvailableDrive(ByRef strDriveLetter As String) As Boolean
Dim intStartLetter As Integer = Asc("A")
Dim intEndLetter As Integer = Asc("Z")
Dim intDriveType As Integer = 0
Dim blnDriveAvailable As Boolean
Dim strShareName As String = ""
Dim blnOpenDriveFound As Boolean = False
For intDriveLetter As Integer = intStartLetter To intEndLetter
strDriveLetter = Chr(intDriveLetter) & ":"
intDriveType = GetDriveType(strDriveLetter & "\")
If intDriveType = DRIVE_NO_ROOT_DIR OrElse intDriveType = DRIVE_REMOTE Then
blnDriveAvailable = DriveIsAvailable(strDriveLetter, strShareName)
If blnDriveAvailable Then
blnOpenDriveFound = True
Exit For
End If
End If
Next
If blnOpenDriveFound = False Then
strDriveLetter = Nothing
End If
Return blnOpenDriveFound
End Function
Private Function DriveIsAvailable(ByVal strDriveLetter As String, ByRef strShareName As String) As Boolean
Const MAX_PATH As Int32 = 260
Dim sbShareNm As New StringBuilder(MAX_PATH)
Dim intLength As Int32 = MAX_PATH
Dim intReturnCode As Integer = 0
intReturnCode = WNetGetConnection(strDriveLetter, sbShareNm, intLength)
If intReturnCode = NO_ERROR Then
strShareName = sbShareNm.ToString
Return False
ElseIf intReturnCode = ERROR_NOT_CONNECTED Then
Return True
Else
Throw New System.ComponentModel.Win32Exception(intReturnCode)
End If
End Function
Private Function GetAllDrivesInfo() As NameValueCollection
Dim intStartLetter As Integer = Asc("A")
Dim intEndLetter As Integer = Asc("Z")
Dim intDriveType As Integer = 0
Dim blnDriveAvailable As Boolean
Dim strDriveLetter As String = ""
Dim strShareName As String = ""
Dim colDriveInfo As New NameValueCollection
For intDriveLetter As Integer = intStartLetter To intEndLetter
strDriveLetter = Chr(intDriveLetter) & ":"
intDriveType = GetDriveType(strDriveLetter & "\")
Select Case intDriveType
Case DRIVE_UNKNOWN, DRIVE_REMOVABLE, DRIVE_FIXED, DRIVE_CDROM, DRIVE_RAMDISK
colDriveInfo.Add(strDriveLetter, "")
Case DRIVE_NO_ROOT_DIR, DRIVE_REMOTE
blnDriveAvailable = DriveIsAvailable(strDriveLetter, strShareName)
If blnDriveAvailable Then
colDriveInfo.Add(strDriveLetter, "")
Else
colDriveInfo.Add(strDriveLetter, strShareName)
End If
End Select
Next
Return colDriveInfo
End Function
|
|
|
|
|
Hello there!
First of all, thanks for the article. It solved one big problem I was facing.
Now, it's my time to contribute with it...
According to Miscrosoft, we can have connection without the localname. You connection code works just fine with it, however, it fails on the disconnect. According to the documentation at http://msdn.microsoft.com/library/default.asp?url=/library/en-us/wnet/wnet/wnetcancelconnection2.asp,
we can replace the localname with the path connected.
I have changed it and this is working just fine with me. I'm posting it
private void zUnMapDrive(bool pfForce)
{
//call unmap and return
int iFlags=0;
if(lf_Persistent){iFlags+=CONNECT_UPDATE_PROFILE;}
int i = WNetCancelConnection2A(ls_Drive, iFlags, Convert.ToInt32(pfForce));
if (i != 0)
i = WNetCancelConnection2A(ls_ShareName, iFlags, Convert.ToInt32(pfForce));
if(i>0){throw new System.ComponentModel.Win32Exception(i);}
}
Hope this helps someone.
Regards,
Joao
|
|
|
|
|
Hello , Thanks for that, I have updated my source and will post an update to CodeProject before long (it will be build 0016). I have made a note of your contribution in the source, but since I dont have your name, its credited to jsantos98
----------------------------
Adam Woods
|
|
|
|
|
Hello Adam,
Thanks for the credit.
My name is "Joao Santos"
Regards,
Joao
|
|
|
|
|
Hi there, this hasn't workd for me. I fetched the code from your homepage. Ran the solution directly, tried mapping with no drive letter (which worked) and then unmapping which didn't work.
tried different servers, including windows 2003 and no luck.
|
|
|
|
|
Sorry for that. The interface has a minor bug
missing the line
oNetDrive.ShareName = txtAddress.Text;
in the button4_Click - disconnect event.
after realizing that, it works fine.
Allegro
|
|
|
|
|
I had this same issue, except mine was not working when passing share name for WNetCancelConnection2. I changed it to pass the local drive and it worked fine. I am testing in win2003 and xp.
In function unMapDrive():
iRet = WNetCancelConnection2(_localDrive, iFlags, System.Convert.ToInt32(_force));
Hope this helps anyone who may have had my same issue.
|
|
|
|
|
Thanks for posting this. It works where some other code fails. This code allows the DOMAIN to be supplied in the logon credential details allowing cross domain drive mapping.
ps. I was intested how this class worked
|
|
|
|
|
Hi I was trying to use this class, after some experimentation on my own I was able to figure out how it works but I then tried your class and i'm running into the same problem. It always returns the following error message:
An exception occurred while connecting: System.ComponentModel.Win32Exception: Th
e network name cannot be found
at aejw.Network.NetworkDrive.zMapDrive(String psUsername, String psPassword)
in c:\Documents and Settings\jmarshall\My Documents\SharpDevelop Projects\EventA
rchiver\cNetworkDrives0015.cs:line 173
at aejw.Network.NetworkDrive.MapDrive(String Username, String Password) in c:
\Documents and Settings\jmarshall\My Documents\SharpDevelop Projects\EventArchiv
er\cNetworkDrives0015.cs:line 129
at EventArchiver.MainClass.Main(String[] args) in c:\Documents and Settings\j
marshall\My Documents\SharpDevelop Projects\EventArchiver\Main.cs:line 80
Any idea whats going on?
ps. the reason it is under this post isntead of new thread is the new thread button doesn't seem to be working for me.
Thanks!
|
|
|
|
|