Jag vill mappa alla enheter på datorn med api och få fram betäckningen Det du skall titta på är DOS-kommandot SUBST.EXE. Vet inte om det finns något motsvarande api för detta dock. > "betäckningen" är nått helt annat än vad du är ute efter :-) Enklast är väl att använda FSO: typoe in my last inlägg...Mappa enheter på pc?
ex:
A:
C:
D:
Får inte denna att snurra, type not defind...
[AuxGetNumDevs]Sv: Mappa enheter på pc?
Ex: SUBST D: "C:\mina dokument"
Så från vb kan du skriva SHELL "SUBST D: ""C:\mina dokument""Sv: Mappa enheter på pc?
Kör denna kod
<code>
'En ListBox en Command1
Private Declare Function GetDriveType Lib "kernel32" _
Alias "GetDriveTypeA" (ByVal nDrive As String) As Long
Private Sub Command1_Click()
' 2 = Floppy 3 = HD 5 = CD
Dim i As Long, ret As Long
For i = 65 To 90 ' A - Z
ret = GetDriveType(Chr$(i) & ":\")
If ret <> 1 Then _
List1.AddItem ret & " " & Chr$(i)
Next 'i
End Sub
</code>Sv: Mappa enheter på pc?
I use these two methods in projects i need to use net drives...
Function Map(DriveLetter As String, ServerShare As String)
Dim netstring As String
netstring = "net use " & DriveLetter & " " & ServerShare '& " /persist:Yes"
Shell netstring
'net use x: \\servername\sharfolder
End Function
Function Disconnect(DriveLetter As String)
Dim netstring As String
netstring = "net use " & DriveLetter & " /d /Y"
Shell netstring
End Function
call them like this...
Map "\\netpc\netdrive\somefolder\" , "G:"
Disconnect "G:"
before doing anything inth new mapped drive you want to make sure it exists first, as it can take some time for the drive to be mapped in the OS.
hope this helps, PaulSv: Mappa enheter på pc?
<code>
Function GetDriveType(DriveType As DriveTypeConst) As String
Select Case DriveType
Case Removable
GetDriveType = "Removable"
Case Fixed
GetDriveType = "Fixed"
Case Remote
GetDriveType = "Remote"
Case CDRom
GetDriveType = "CDRom"
Case RamDisk
GetDriveType = "RamDisk"
Case Else 'Scripting.DriveTypeConst.UnknownType
GetDriveType = "Unknown"
End Select
End Function
Private Sub Form_Load()
Dim FSO As Scripting.FileSystemObject
Dim d As Drive
Set FSO = New Scripting.FileSystemObject
For Each d In FSO.Drives
Debug.Print d.DriveLetter & ":", GetDriveType(d.DriveType)
Next
End Sub
</code>Sv: Mappa enheter på pc?
to map a net drive I use the cal to the method Map
Map "G:", "\\netpc\netdrive\somefolder\"
Also I have a posted in a method I use to allow for a Shell operation to finnish, thus making sure that the SHELL command and all respective functions of the underlying functions have completed before my programs continue...
In this case if you use ShellOperationResult = SHELLAndWait(Map("\\serverX\folderY\","G:")) then the program will be put in hold until the command has been completed by the OS
Add these API declares in same module/class that you have the shellAndWait method
'**********************************************************************
Public Declare Function OpenProcess Lib "kernel32" (ByVal dwDesiredAccess As Long, ByVal bInheritHandle As Long, ByVal dwProcessId As Long) As Long
Public Declare Function WaitForSingleObject Lib "kernel32" (ByVal hHandle As Long, ByVal dwMilliseconds As Long) As Long
Public Declare Function CloseHandle Lib "kernel32" (ByVal hObject As Long) As Long
'**********************************************************************
'Put this method in the same Module/class as the API declarations
Public Function ShellAndWait(ByVal program_name As String) As Double
'Routine to start an external executable and with for the process handle to die.
Dim process_id As Long
Dim process_handle As Long
On Error GoTo ShellError
process_id = Shell(program_name, vbNormalFocus) ' Start the program.
On Error GoTo 0
DoEvents
process_handle = OpenProcess(SYNCHRONIZE, 0, process_id) ' Wait for the program to finish.
If process_handle <> 0 Then ' Get the process handle.
WaitForSingleObject process_handle, INFINITE
CloseHandle process_handle
End If
ShellAndWait = 0
Exit Function
ShellError:
ShellAndWait = Err.Number
End Function
I take no credit for the ShellAndWait method, I got from the vbcode.com site a few years back, It has never let me down.
Here is the SUBST help and parameters the command can take...
--------------------------------------------------------------------------------------------------
SUBST = Associates a path with a drive letter.
SUBST [drive1: [drive2:]path]
SUBST drive1: /D
drive1: Specifies a virtual drive to which you want to assign a path.
[drive2:]path Specifies a physical drive and path you want to assign to
a virtual drive.
/D Deletes a substituted (virtual) drive.
Type SUBST with no parameters to display a list of current virtual drives.
--------------------------------------------------------------------------------------------------
Using NET USE will allow as I do to mapp net drives when required, remembering that user rights, users must have rights to be able to mapp a given drive.
And remeber to check if a drive exists before trying anything as the other tips here show.
Cheers Paul