24

ディレクトリが存在するかどうかを検出しようとしていますが、この特定の状況では、ディレクトリはネットワーク上の場所です。私は VB.NETMy.Computer.FileSystem.DirectoryExists(PATH)とより一般的なを使用System.IO.Directory.Exists(PATH)しましたが、どちらの場合もシステムの応答は false です。確認したところ、PATH が存在し、MyComputer フォルダーで表示できます。プログラムをデバッグしてMy.Computer.FileSystem.Drives変数を監視すると、ネットワークの場所がそのリストに表示されません。

更新:チェックしたところ、Windows XP では応答が True ですが、Windows 7 ではそうではありませんでした。

更新 2:提案された両方のソリューションをテストしましたが、まだ同じ問題があります。下の画像では、エクスプローラーを使用してアクセスできますが、プログラムはアクセスできません。関数はGetUNCPath有効なパス (エラーなし) を返しますが、それでもDirectory.Existsfalse を返します。

UNCパス「\\Server\Images」も試しました。同じ結果です。

ここに画像の説明を入力

UPDATE3: ネットワーク ドライブにリンクできない場合、UNC パスに直接リンクするにはどうすればよいですか? VS を通常モードで実行すると機能することを発見しましたが、ソフトウェアは管理者モードで実行する必要があります。では、管理者としてネットワーク ディレクトリの存在を確認する方法はありますか?

4

4 に答える 4

17

UAC がオンになっている場合、マップされたネットワーク ドライブは、マップされているセッション (通常または昇格) に「既定で」のみ存在します。エクスプローラーからネットワーク ドライブをマップし、VS を管理者として実行すると、ドライブは存在しません。

MS が「リンクされた接続」と呼ぶものを有効にする必要があります: HKEY_LOCAL_MACHINE\Software\Microsoft\Windows\CurrentVersion\Policies\System: EnableLinkedConnections (REG_DWORD) = 0x1

UAC を使用した「2 つのログオン セッション」に関する背景情報: http://support.microsoft.com/kb/937624/en-us

于 2013-07-17T15:40:00.613 に答える
8

を使用するSystem.IO.Directory.Existsと、ディレクトリが見つからなかったことだけが通知されますが、これは、ディレクトリが実際には存在しないか、ユーザーがディレクトリへの十分なアクセス権を持っていないことが原因である可能性があります。

これを解決するために、ディレクトリが存在しない本当の理由を取得できなかった後に二次テストを追加し、これを標準メソッドDirectory.Existsの代わりに使用されるグローバル メソッドにラップしました。Directory.Exists

''' <summary>
''' This method tests to ensure that a directory actually does exist. If it does not, the reason for its 
''' absence will attempt to be determined and returned. The standard Directory.Exists does not raise 
''' any exceptions, which makes it impossible to determine why the request fails.
''' </summary>
''' <param name="sDirectory"></param>
''' <param name="sError"></param>
''' <param name="fActuallyDoesntExist">This is set to true when an error is not encountered while trying to verify the directory's existence. This means that 
''' we have access to the location the directory is supposed to be, but it simply doesn't exist. If this is false and the directory doesn't exist, then 
''' this means that an error, such as a security error, was encountered while trying to verify the directory's existence.</param>
Public Function DirectoryExists(ByVal sDirectory As String, ByRef sError As String, Optional ByRef fActuallyDoesntExist As Boolean = False) As Boolean
    ' Exceptions are partially handled by the caller

    If Not IO.Directory.Exists(sDirectory) Then
        Try
            Dim dtCreated As Date

            ' Attempt to retrieve the creation time for the directory. 
            ' This will usually throw an exception with the complaint (such as user logon failure)
            dtCreated = Directory.GetCreationTime(sDirectory)

            ' Indicate that the directory really doesn't exist
            fActuallyDoesntExist = True

            ' If an exception does not get thrown, the time that is returned is actually for the parent directory, 
            ' so there is no issue accessing the folder, it just doesn't exist.
            sError = "The directory does not exist"
        Catch theException As Exception
            ' Let the caller know the error that was encountered
            sError = theException.Message
        End Try

        Return False
    Else
        Return True
    End If
End Function
于 2013-07-04T17:10:12.413 に答える
5
public static class MappedDriveResolver
    {
        [DllImport("mpr.dll", CharSet = CharSet.Unicode, SetLastError = true)]
        public static extern int WNetGetConnection([MarshalAs(UnmanagedType.LPTStr)] string localName, [MarshalAs(UnmanagedType.LPTStr)] StringBuilder remoteName, ref int length);
        public static string GetUNCPath(string originalPath)
        {
            StringBuilder sb = new StringBuilder(512);
            int size = sb.Capacity;

            // look for the {LETTER}: combination ...
            if (originalPath.Length > 2 && originalPath[1] == ':')
            {
                // don't use char.IsLetter here - as that can be misleading
                // the only valid drive letters are a-z && A-Z.
                char c = originalPath[0];
                if ((c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z'))
                {
                    int error = WNetGetConnection(originalPath.Substring(0, 2), sb, ref size);
                    if (error == 0)
                    {
                        DirectoryInfo dir = new DirectoryInfo(originalPath);
                        string path = Path.GetFullPath(originalPath).Substring(Path.GetPathRoot(originalPath).Length);
                        return Path.Combine(sb.ToString().TrimEnd(), path);
                    }
                }
            }    
            return originalPath;
        }
    }

これを使用するには、ネットワーク フォルダー パスを渡し、UNC フォルダー パスに変換して、フォルダーが存在するかどうかを確認します。

File.Exists(MappedDriveResolver.GetUNCPath(filePath));

編集:

私はあなたの2番目の編集と唯一の違い(私のWindows7で)を見ました.ネットワークドライブを表示すると、Computer > Images (\\xyzServer) . あなたのPCの名前はEquipo?そのチームはスペイン語ですか?それはあなたのPCですか?私はあなたの問題を再現しようとしましたが、私にとってはうまくいきます:

ここに画像の説明を入力

于 2013-07-05T05:08:06.790 に答える