从共享文件夹运行桌面应用程序时出现 UnauthorizedAccessException

发布于 2024-08-30 01:24:08 字数 1876 浏览 3 评论 0原文

我使用 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”

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

    '我的代码:

    将 src 变暗为字符串 = mcGlobals.cmcFiles.mcGetFileNameOcx()
    调暗 ioStream 作为新的 System.IO.FileStream(src, IO.FileMode.Open)

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

    公共共享函数 mcGetFileNameOcx() 作为字符串

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

     Dim dirName As String = Application.StartupPath & “\”
      Dim sFiles() As String = System.IO.Directory.GetFiles(dirName, "*.ocx")
    
      将 i 调暗为整数
      对于 i = 0 至 UBound(sFiles)
        调试.WriteLine(System.IO.Path.GetFullPath(sFiles(i)))
        ' 如果找到任何 - 返回第一个:
        返回 System.IO.Path.GetFullPath(sFiles(i))
      下一个
      返回 ”” 
    
    结束功能
    

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

    ' 我收到的异常:

    System.UnauthorizedAccessException:对路径“\\computerName\sharedFolderName\my.ocx”的访问被拒绝。
      在 System.IO._Error(Int32 errorCode, String MaybeFullPath)
      在 System.IO.FileStream.Init(...)
      在 System.IO.FileStream..ctor(...)
      在 System.IO.FileStream..ctor(字符串路径,FileMode 模式)
    

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

I created a desktop application using VS 2008.

When I run it locally, all works well.

I shared my output folder (WITHOUT allowing network users to change my files)
and ran my exe from another Vista computer on our intranet.

When running the shared exe, I receive "System.UnauthorizedAccessException" when trying to read a file.

How can I give permission to allow reading the file?
Should I change the code?
Should I grant permission to the application\folder on the Vista computer? how?

Notes:

  • I do not use ClickOnce. the application should be distributed using xcopy.

  • My application target framework is ".Net Framework 2.0"

  • On the Vista computer, "controlPanel | UninstallOrChangePrograms" it says it has "Microsoft .Net Framework 3.5 SP1"

  • I also tried to map the folder drive, but got the same errors, only now the fileName is "T:\my.ocx"

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

    ' my code:

    Dim src As String = mcGlobals.cmcFiles.mcGetFileNameOcx()
    Dim ioStream As New System.IO.FileStream(src, IO.FileMode.Open)

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

    Public Shared Function 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
    

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

    ' The Exception I receive:

    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)
    

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自找没趣 2024-09-06 01:24:08

找到了。

根据 MSDN,[FileStream 构造函数 (String, FileMode)]

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

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

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

所以我添加了 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) 

found it.

According to MSDN, [FileStream Constructor (String, FileMode) ]

. . . For constructors without a FileAccess parameter,

  • if the mode parameter is set to Append, Write is the default access.
  • Otherwise, the access is set to ReadWrite.

-> my code used the default, with ioStream.CanWrite:=True, and I do not have Write permission on the shared folder.

so I added FileAccess parameter:

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) 
请爱~陌生人 2024-09-06 01:24:08

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

参考文献

Starting with .NET Framework 3.5 SP1 you are allowed to run an application from a network share. You don't have to target it, you just have to have it installed.

References

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文