Binary file open error

T

Thomas Scheiderich

The following code works fine. It opens file reads the data and then
closes.

****************************************************************************
****************
Dim objFileInfo As FileInfo = new FileInfo(strFilePath)
Dim objStream as StreamReader = objFileInfo_OpenText()
Dim strContents as String = objStream.ReadToEnd()
objStream.Close()
****************************************************************************
****************

I get an error on the following line with the same file.

Dim fs as FileStream = new FileStream(strFilePath, FileMode.Open)

The error I get is:

****************************************************************************
****************
Access to the path "c:\QA Docs\QPM 0.00 Table of Contents.doc" is denied.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.UnauthorizedAccessException: Access to the path
"c:\QA Docs\QPM 0.00 Table of Contents.doc" is denied.

ASP.NET is not authorized to access the requested resource. Consider
granting access rights to the resource to the ASP.NET request identity.
ASP.NET has a base process identity (typically {MACHINE}\ASPNET on IIS 5 or
Network Service on IIS 6) that is used if the application is not
impersonating. If the application is impersonating via <identity
impersonate="true"/>, the identity will be the anonymous user (typically
IUSR_MACHINENAME) or the authenticated request user.

To grant ASP.NET write access to a file, right-click the file in Explorer,
choose "Properties" and select the Security tab. Click "Add" to add the
appropriate user or group. Highlight the ASP.NET account, and check the
boxes for the desired access.
****************************************************************************
****************

Why would I be denied access in this procedure and not the one before?

Thanks,

Tom
 
K

Kevin Spencer

Are they both attempting to open the same file?

--
HTH,
Kevin Spencer
..Net Developer
Microsoft MVP
Big things are made up
of lots of little things.
 
T

Thomas Scheiderich

Kevin Spencer said:
Are they both attempting to open the same file?

They are.

But not at the same time. I am commenting one out when running the other,
just to get it to work.

Tom.
 
J

John Saunders

Thomas Scheiderich said:
The following code works fine. It opens file reads the data and then
closes.

****************************************************************************
****************
Dim objFileInfo As FileInfo = new FileInfo(strFilePath)
Dim objStream as StreamReader = objFileInfo_OpenText()
Dim strContents as String = objStream.ReadToEnd()
objStream.Close()
****************************************************************************
****************

Try the following:

Dim objReader As StreamReader = New StreamReader(strFilePath)
Dim strContents As String = objReader.ReadToEnd()

BTW, what do you mean when you call this a "binary file"? You're reading the
entire thing into a string, which is a "text file" sort of thing to do. If
you really meant to store binary data, you could try:

Dim objStream As FileStream = New FileStream(strFilePath)
Dim objReader As BinaryReader = New BinaryReader(objStream)
'
Dim i As Integer = objReader.ReadInt32()
Dim s As String = objReader.ReadString()
Dim f As Double = objReader.ReadDouble()
 
T

Thomas Scheiderich

John Saunders said:
****************************************************************************
****************************************************************************

Try the following:

Dim objReader As StreamReader = New StreamReader(strFilePath)
Dim strContents As String = objReader.ReadToEnd()

BTW, what do you mean when you call this a "binary file"? You're reading the
entire thing into a string, which is a "text file" sort of thing to do. If
you really meant to store binary data, you could try:

Dim objStream As FileStream = New FileStream(strFilePath)
Dim objReader As BinaryReader = New BinaryReader(objStream)
'
Dim i As Integer = objReader.ReadInt32()
Dim s As String = objReader.ReadString()
Dim f As Double = objReader.ReadDouble()

What I meant by Binary, was that I need to read this into a Binary array
that I am going to put into Sql Server as an image. I finally got it to
work using the following:

Dim fs as FileStream = new
FileStream(strFilePath,FileMode.OpenOrCreate,FileAccess.Read)
Dim arrByteData(objFileInfo.Length) as Byte
fs.Read(arrByteData, 0, fs.Length)
fs.Close()

What I was missing was the FileAccess.Read in the FileStream call (not sure
why that was causing me an error - I assume, it was because without it, I
could be writing also).

I was using OpenText(), because I was copying into a string. I was
originally doing this so I could use the string search functions. But I
can't copy it into Sql Server as a Sring into the image field. I need to
upload it as a Binary ( at least this is the way I was told - I tried
textcopy and that was a disaster).

Tom.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,240
Members
46,830
Latest member
HeleneMull

Latest Threads

Top