FileStream access denied

jvcoach23

Well-known member
Joined
May 22, 2003
Messages
192
Location
Saybrook, IL
I need to read an image into a datasets table. I have found an example and am trying to learn more about it.. but when I run this.. I get an access denied error . Here is the code.

Code:
 Public Shared Sub LoadAllImages(ByVal MyDataTable As DataTable, ByVal FilePathField As String, ByVal ImageField As String)
        loop through all the rows and load the images
        For Each dr As DataRow In MyDataTable.Rows
            LoadImage(dr, ImageField, dr.Item(FilePathField))
        Next
    End Sub
    Public Shared Sub LoadImage(ByVal MyDataRow As System.Data.DataRow, ByVal FilePath As String, ByVal ImageField As String)
        Dim fs As New System.IO.FileStream(FilePath, IO.FileMode.Open, System.IO.FileAccess.Read)
        Dim Image(fs.Length) As Byte
        fs.Read(Image, 0, fs.Length)
        fs.Close()
        MyDataRow.Item(ImageField) = Image

    End Sub
and what Im using to call this
Code:
LoadAllImages(ds.Tables(0), "vcFileName", "d:\Image")
when i step through, my access denied error happens on the dim fs as new system.... line. The value coming from the dataset in teh vcFileName column is p01983.jpg. d:\Image is an actaul path.. inside that path is the image file. I am logged in as Administrator of this box and that path is local to my machine. can someone help me understand what Im doing wrong and right it please.

thanks
shannon
 
Are you doing this from an ASP.Net application? If so then you need to make sure your web application has access rather than your login account.
Search these forums for several possible solutions.
 
Just noticed you are using
Code:
d:Image
rather than
Code:
d:\Image
in your sample above - is that a type here or in your real code? If that doesnt fix the problem could you post the exact exception type / message that is being thrown along with the full path that it is attempting to read from.
 
typeo.. Here is the error message that Im getting.
[System.UnauthorizedAccessException]: {System.UnauthorizedAccessException}
HelpLink: Nothing
InnerException: Nothing
Message: "Access to the path "d:\Image" is denied."
Source: "mscorlib"

Here is the code I changed so that I could get the try and catch to work
Code:
Public Shared Sub LoadImage(ByVal MyDataRow As System.Data.DataRow, ByVal FilePath As String, ByVal ImageField As String)
        Dim fs As FileStream
        Dim fs As New System.IO.FileStream(FilePath, IO.FileMode.Open, System.IO.FileAccess.Read)
        Try
            fs = New FileStream(FilePath, IO.FileMode.Open, System.IO.FileAccess.Read, FileShare.Read)
        Catch ex As Exception
            Throw ex
        End Try

        Dim Image(fs.Length) As Byte
        fs.Read(Image, 0, fs.Length)
        fs.Close()
        MyDataRow.Item(ImageField) = Image

    End Sub
sure hope you can help me out. Im getting the error in the windows form on the fs=new filestream line.

thanks
shannon
 
I got it working. what I thought I was suppose to be passing in was the path on the
Code:
        LoadAllImages(ds.Tables(0), "vcFileName", "d:\Image")

what it was really looking for was the column that I was going to be stuff the image into. I still dont think I have the image stuffing going quite right.. but one step further.

it was suppose to look like this
Code:
        LoadAllImages(ds.Tables(0), "vcFileName", "Image")

thanks
shannon
 
Back
Top