Question

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)
    

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

Was it helpful?

Solution

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) 

OTHER TIPS

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

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top