2

準備ができていないドライブを処理するためのより良いソリューションが必要であり、rwドライブ内のファイルを表示および変更できるようにしたいと考えています。残念ながら、それは常にドライブの準備ができていないエラーを出し、私ができる唯一のことはエラーを処理することです。

これまで私はこれを行いました:

マイドライブ:

Private Sub imperialdrive_Change()
    On Error GoTo I_have_a_baad_feeling_about_this
    imperialdir.Path = RootPathOnDrive(imperialdrive.Drive)
    Exit Sub

I_have_a_baad_feeling_about_this:
    If Err.Number = 68 Then
        MsgBox "The selected drive is not available at the moment.", vbOKOnly, "I feel a disturbance in the force."
    Else
        MsgBox Err.Number & " " & Err.Description, vbCritical, "There is a Bounty Hunter here."
    End If
End Sub

私の機能:

'Such a bad choise for a function name
'It sounds like doing smt more than changing the name of drive lol
Public Function RootPathOnDrive(ByVal Drive)
    'So how it comes as "k:" instead of "k:\" Is it really cause its not ready? Both ways i should try reaching "k:\"
     RootPathOnDrive = Left(Drive, 1) & ":\"
End Function
4

2 に答える 2

1

Microsoft Scripting Runtime(scrrun.dll)の一部であるFileSystemObjectの使用を検討しましたか?

Public Function CheckDrivePathReady(IN_sPath as String) As Boolean
    Dim myFSO As Scripting.FileSystemObject
    Dim myDrive As Scripting.Drive
    Dim myDriveName As String

    'Create a new FileSystemObject
    Set myFSO = New Scripting.FileSystemObject

    'Get drive name from path.
    myDriveName = gFSO.GetDriveName(IN_sPath)
    'Create a "Drive" object to test the properties of.
    Set myDrive = gFSO.GetDrive(myDriveName)

    'Test if the drive is usable.
    '(there are more properties than just "ready" that can be tested)
    If myDrive.IsReady Then
        'Work with ready drive here....

    End If

    'Make sure to clean up when done.
    set myDrive = Nothing
    Set myFSO = Nothing
End Function

プロジェクト参照にMicrosoftScriptingRuntimeを含める必要がありますが、ドライブとパスを操作する場合、FileSystemObjectは非常に貴重であることがわかりました。

于 2012-02-28T00:40:54.220 に答える
0

imperialdirとは何ですか?たとえば、それはファイルリストボックスですか?「k:\」のようなパスが存在することがわかっている場合でも、Pathプロパティを設定しようとすると、常にエラーがスローされるという問題はありますか?一部のオブジェクトには、読み取り専用のPathプロパティがあります。関数CheckDriveのより適切な名前はRootPathOnDriveです。

于 2012-02-08T19:27:27.257 に答える