0

我创建了 .exe 文件来检查共享路径上是否存在文件..

下面是vb.net代码

在这里,问题是..在提到的共享路径上没有访问权限的用户他们无法检查状态,他们必须以具有访问权限的不同用户 ID 运行 exe...

如果我可以在代码中绑定凭据以便每个人都可以检查状态的任何解决方案..???

Try

Dim curFile As String = "\\SharedPath\Data\testFile.txt"
    If (File.Exists(curFile) ) Then
        lblFileStatus.Text = "Received"
    Else
        lblFileStatus.Text = "File not Received"
    End If
    Catch ex As Exception

End Try
4

1 回答 1

1

完全不推荐下面的方法,我完全同意上面的评论,但作为一个纯粹的exersize你可以试试这个。

首先声明对 Windows API 的调用:

#Region "Windows API Declarations"

    'used in calling WNetAddConnection2
    <StructLayout(LayoutKind.Sequential)> Private Structure NETRESOURCE
        Public dwScope As Integer
        Public dwType As Integer
        Public dwDisplayType As Integer
        Public dwUsage As Integer
        <MarshalAs(UnmanagedType.LPStr)> Public lpLocalName As String
        <MarshalAs(UnmanagedType.LPStr)> Public lpRemoteName As String
        <MarshalAs(UnmanagedType.LPStr)> Public lpComment As String
        <MarshalAs(UnmanagedType.LPStr)> Public lpProvider As String
    End Structure

    'WIN32API - WNetAddConnection2

    ''' <summary>
    ''' Helper function to call Win32API function
    ''' </summary>
    ''' <param name="i_sPath">Path to a file to authenticate</param>
    ''' <param name="i_sPassword">User ID</param>
    ''' <param name="i_sUserID">Password</param>
    ''' <remarks></remarks>
    Public Shared Sub WNetAddConnection2AEx(ByVal i_sPath As String, ByVal i_sPassword As String, ByVal i_sUserID As String)
        Dim nr(1) As NETRESOURCE
        nr(0).lpRemoteName = i_sPath.Substring(0, i_sPath.IndexOf("\", 2))
        nr(0).lpLocalName = "" 'local mashine
        nr(0).dwType = 1 'disk
        nr(0).dwDisplayType = 0
        nr(0).dwScope = 0
        nr(0).dwUsage = 0
        nr(0).lpComment = ""
        nr(0).lpProvider = ""
        Dim iErr As Integer = WNetAddConnection2A(nr, i_sPassword, i_sUserID, 0)
        If iErr > 0 Then Throw New Exception("Can not connect to share folder: " & i_sPath)
    End Sub

    <DllImport("mpr.dll")> _
    Private Shared Function WNetAddConnection2A( _
    <MarshalAs(UnmanagedType.LPArray)> ByVal lpNetResource As NETRESOURCE(), _
    <MarshalAs(UnmanagedType.LPStr)> ByVal lpPassword As String, _
    <MarshalAs(UnmanagedType.LPStr)> ByVal lpUserName As String, _
    ByVal dwFlags As Integer) As Integer

    End Function

    <DllImport("mpr.dll")> _
    Private Shared Function WNetCancelConnection2A( _
    <MarshalAs(UnmanagedType.LPStr)> ByVal lpName As String, _
    ByVal dwFlags As Integer, ByVal fForce As Integer) As Integer

    End Function

    '{**** Declare signatures for Win32 LogonUser and CloseHandle APIs
    <DllImport("advapi32.dll", SetLastError:=True)> _
    Private Shared Function LogonUser(ByVal userID As String, _
                                      ByVal domain As String, _
                                      ByVal password As String, _
                                      ByVal logonType As LogonSessionType, _
                                      ByVal LogonProv As LogonProvider, _
                                      ByRef token As IntPtr) As Boolean
    End Function

    <DllImport("kernel32.dll", SetLastError:=True)> _
    Private Shared Function CloseHandle(ByVal handle As IntPtr) As Boolean

    End Function

    Private Enum LogonSessionType As Integer
        Interactive = 2
        Network
        Batch
        Service
        NetworkCleartext = 8
        NewCredentials
    End Enum

    Private Enum LogonProvider As Integer
        WinDefault = 0  'default for platform (use this!)
        WinNT35       'sends smoke signals to authority
        WinNT40       'uses NTLM
        WinNT50       'negotiates Kerb or NTLM
    End Enum
    '**** }

#End Region

使用这样的声明,您可以进行以下调用:

'Open secondary login with different credentials
WNetAddConnection2AEx(curFile, sPassword, sUserID)

'*** Here do your stuff with the sPath

'Cleanup
WNetCancelConnection2A(curFile.Substring(0, curFile.IndexOf("\", 2)), 0, -1)
于 2013-06-06T19:52:46.863 回答