|
-
Jun 27th, 2011, 09:03 AM
#1
Thread Starter
Addicted Member
Access Denied when Reading File from CD
Hey guys,
I'm having trouble reading a file into a filestream from a CD. I get 'Access to the path denied'. Works fine when reading a text file from a local drive, but getting error from CD.
Code:
Dim fs As New IO.FileStream(filename, IO.FileMode.Open)
-
Jun 27th, 2011, 09:46 AM
#2
Re: Access Denied when Reading File from CD
Are you running the code as an administrator?
-
Jun 27th, 2011, 09:52 AM
#3
Thread Starter
Addicted Member
Re: Access Denied when Reading File from CD
Thanks for your response. Yes Im running as Administrator, but I didn't even think you needed admin access to read a file from a CD. (That even a non-privileged user would be able to open in another app)
-
Jun 27th, 2011, 09:52 AM
#4
Re: Access Denied when Reading File from CD
FileStream objects can both read and write files. If you don't specify whether you want to read or write, it looks like it assumes you want both. Since you can't write to a file on CD, it fails.
You need to use the constructor that takes a FileAccess parameter:
Code:
Dim fs As New IO.FileStream(filename, FileMode.Open, FileAccess.Read)
If you're just trying to read a text file, it's probably a better idea to use something like File.OpenText(); that way you don't have to worry about the low-level details.
-
Jun 27th, 2011, 10:26 AM
#5
Thread Starter
Addicted Member
Re: Access Denied when Reading File from CD
Thanks Sitten. That did get me past my original error, but now I am getting an error on the next line when trying to read the StreamReader.
Code:
Dim tableData() As String = sr.ReadToEnd().Split(vbLf)
This line gives me an error of: "Handle does not support synchronous operations. The parameters to the FileStream constructor may need to be changed to indicate that the handle was opened asynchronously.
I am using a background worker to run this method. Any idea why just adding the FileAccess.Read parameter would make it throw this error?
Thanks
-
Jun 27th, 2011, 10:34 AM
#6
Re: Access Denied when Reading File from CD
Try this:
Code:
'//specifies that the stream is async
Dim stream = New IO.FileStream(filename, IO.FileMode.Open, _
IO.FileAccess.Read, IO.FileShare.None, _
4096, True)
-
Jun 27th, 2011, 11:05 AM
#7
Thread Starter
Addicted Member
Re: Access Denied when Reading File from CD
Thanks FA. Sorry to be a pain, but that did get me past that error to a new error. When attempting to read from the Stream (sr.ReadToEnd), I'm getting an error of "The parameter is incorrect"
Code:
Dim fs = New IO.FileStream(filename, IO.FileMode.Open, _
IO.FileAccess.Read, IO.FileShare.None, _
4096, True)
Dim sr As New IO.StreamReader(fs)
Dim tableData() As String = sr.ReadToEnd().Split(vbLf)
-
Jun 27th, 2011, 11:48 AM
#8
Re: Access Denied when Reading File from CD
Just out of curiousity, if you try and read the file like this:
Code:
Dim contents = IO.File.ReadAllText(filename)
Does it work?
-
Jun 27th, 2011, 01:48 PM
#9
Thread Starter
Addicted Member
Re: Access Denied when Reading File from CD
That puts me back at the :
"Handle does not support synchronous operations. The parameters to the FileStream constructor may need to be changed to indicate that the handle was opened asynchronously."
But I dont see an option to run async with the ReadAllText method.
-
Jun 27th, 2011, 04:20 PM
#10
Re: Access Denied when Reading File from CD
When using the code in #10, which parameter did it say was incorrect? You usually get a parameter name along with that. You're going to have to use one of the FileStream constructors that has a boolean useAsync parameter, and that's the only one that takes a string for a filename. There's some more exotic approaches that might work but I'd like to avoid them if possible.
-
Jun 29th, 2011, 02:05 PM
#11
Thread Starter
Addicted Member
Re: Access Denied when Reading File from CD
Thanks Sitten. The only thing the exception says is "The parameter is incorrect".
Here's a bit of the stack trace from when it tries to Read:
Code:
System.IO.IOException was unhandled
Message="The parameter is incorrect. "
Source="mscorlib"
StackTrace:
at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
at System.IO.FileStream.EndRead(IAsyncResult asyncResult)
at System.IO.FileStream.ReadCore(Byte[] buffer, Int32 offset, Int32 count)
at System.IO.FileStream.Read(Byte[] array, Int32 offset, Int32 count)
at System.IO.StreamReader.ReadBuffer()
at System.IO.StreamReader.ReadToEnd()
-
Jun 30th, 2011, 11:35 AM
#12
Re: Access Denied when Reading File from CD
I have tried and successfully opened a text file on a CD using all of the following techniques:
File.ReadAllText(); much easier for smaller files:
Code:
Dim text As String = File.ReadAllText(FilePath)
Console.WriteLine(text)
Use of a StreamReader initialized from a path:
Code:
Using reader As New StreamReader(FilePath)
Dim text As String = reader.ReadToEnd()
Console.WriteLine(text)
End Using
Using the lowest-level approach; opening a FileStream, handing it to a StreamReader, and reading it line by line:
Code:
Using fs As New FileStream(FilePath, FileMode.Open, FileAccess.Read)
Using reader As New StreamReader(fs)
While Not reader.EndOfStream
Console.WriteLine(reader.ReadLine())
End While
End Using
End Using
All worked without a hitch. This leads me to believe there's something else wrong with your code, something wrong with your CD media, or something wrong with your drive.
There's 2 environmental differences that may be at play. How large is the text file you are reading? Mine was 136 KB. If yours is significantly larger, there's a higher probability you'll exaust any caches in your drive and trigger a problem with the media/drive/driver. If you tell me the filesize, I can try to reproduce that scenario and figure out if it's a problem specific to your machine.
The other environmental difference is the rest of your code. I noticed you aren't using Using statements or calling Dispose() on these file handles. It's possible you're trying to open the file multiple times, but the handle hasn't been closed yet. Don't do that.
So the ball's in your court; what's the size of the file and how are you guaranteeing you close your handles?
-
Jun 30th, 2011, 12:06 PM
#13
Thread Starter
Addicted Member
Re: Access Denied when Reading File from CD
I actually wasn't using "Using", but I just gave it a shot, and am still getting the same error. I was originally told about the problem by someone testing the app on a Client machine, and was able to reproduce it.
I thought it may have been an issue because that part of the code was in a method inside a background worker, but am getting the same issue when I moved it outside of the bg worker. Now its simply a piece of code that executes from a button click event, has an open file dialog, and tries to read the file. I'm starting to think it could be an issue with maybe some of the references im using on this form not cooperating, Im gonna try a new project and tweak around with it some.
Thanks for your help
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|