【问题标题】:UnauthorizedAccessException when running desktop application from shared folder从共享文件夹运行桌面应用程序时出现 UnauthorizedAccessException
【发布时间】:2010-04-22 13:25:19
【问题描述】:

我使用 VS 2008 创建了一个桌面应用程序。

当我在本地运行时,一切正常。

我共享了我的输出文件夹(不允许网络用户更改我的文件) 并从我们 Intranet 上的另一台 Vista 计算机上运行我的 exe。

运行共享 exe 时,我在尝试读取文件时收到“System.UnauthorizedAccessException”。

如何授予允许读取文件的权限? 我应该更改代码吗? 我应该授予对 Vista 计算机上的应用程序\文件夹的权限吗?怎么样?

注意事项:

  • 我不使用 ClickOnce。应用程序应使用 xcopy 分发。

  • 我的应用目标框架是“.Net Framework 2.0”

  • 在 Vista 计算机上,“controlPanel | UninstallOrChangePrograms”显示“Microsoft .Net Framework 3.5 SP1”

  • 我也尝试映射文件夹驱动器,但得到了同样的错误,只是现在文件名是“T:\my.ocx”

    ' ---------------------------------- ------------------------------------

    ' 我的代码:

    Dim src As String = mcGlobals.cmcFiles.mcGetFileNameOcx()
    将 ioStream 调暗为新 System.IO.FileStream(src, IO.FileMode.Open)

    ' ---------------------------------- ------------------------------------

    公共共享函数 mcGetFileNameOcx() As String

    ' ---------------------------------- ------------------------------------

      Dim dirName As String = Application.StartupPath & "\"
      Dim sFiles() As String = System.IO.Directory.GetFiles(dirName, "*.ocx")
    
      Dim i As Integer
      For i = 0 To UBound(sFiles)
        Debug.WriteLine(System.IO.Path.GetFullPath(sFiles(i)))
        ' if found any - return the first:
        Return System.IO.Path.GetFullPath(sFiles(i))
      Next
      Return "" 
    
    End Function
    

    ' ---------------------------------- ------------------------------------

    ' 我收到的异常:

    System.UnauthorizedAccessException: Access to the path '\\computerName\sharedFolderName\my.ocx' is denied.
      at System.IO._Error(Int32 errorCode, String maybeFullPath)
      at System.IO.FileStream.Init(...)
      at System.IO.FileStream..ctor(...)
      at System.IO.FileStream..ctor(String path, FileMode mode)
    

    ' ---------------------------------- ------------------------------------

【问题讨论】:

    标签: .net windows-vista unauthorizedaccessexcepti


    【解决方案1】:

    找到了。

    根据 MSDN,[FileStream Constructor (String, FileMode)]

    。 . .对于没有 FileAccess 参数的构造函数,

    • 如果 mode 参数设置为 Append,则 Write 是默认访问。
    • 否则,访问权限设置为 ReadWrite

    ->我的代码使用了默认值,ioStream.CanWrite:=True,我没有共享文件夹的Write权限。

    所以我添加了 FileAccess 参数:

    new code:
    Dim ioStream As New System.IO.FileStream(srcOcx, IO.FileMode.Open, IO.FileAccess.Read) 
    
    old code:
    Dim ioStream As New System.IO.FileStream(srcOcx, IO.FileMode.Open) 
    

    【讨论】:

      【解决方案2】:

      .NET Framework 3.5 SP1 开始,您可以从网络共享运行应用程序。您不必定位它,只需安装它即可。

      参考文献

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2023-03-21
        • 1970-01-01
        • 1970-01-01
        • 2014-05-23
        • 2013-05-08
        • 2018-08-02
        相关资源
        最近更新 更多