?
Solved

FileStream in VB.net

Posted on 2006-05-13
3
Medium Priority
?
6,995 Views
Last Modified: 2008-01-09
I have some questions regarding the code below:

Dim sdirectory as string="C:\vb.net\files"
dim sfile as string="example.txt"
dim spath as string= sdirectory & sfile
dim sfilestrean as filestream
sfilestream=new filestream(spath,filemode.open,fileaccess.readwrite)


1-can I just use: dim spath as string="C:\vb.net\files\example.txt" instead of declaring Dim sdirectory as string="C:\vb.net\files"  then dim sfile as string="example.txt"?

2-when I put filemode.open, Fileaccess.write or Fileaccess.readwrite or Fileaccess.read or Fileaccess.none.   Does this mean if that file exists and accesseb by another user while this code is running can allow/prevent that user from read or write? please explain.


3- when I put filemode.createnew, Fileaccess.write or Fileaccess.readwrite or Fileaccess.read or Fileaccess.none.   Does this mean if that file exists and accesseb by another user while this code is running can allow/prevent that user from read or write? please explain.

Thanks

0
Comment
Question by:jskfan
  • 2
3 Comments
 
LVL 14

Accepted Solution

by:
nayernaguib earned 1600 total points
ID: 16674356
1. Of course you can! Afterall, you never use any of the strings alone, but rather, use the concatenated version. Variable names do not make any difference.

2. This depends upon how you want to access the file. You can have as many users/applications open the same file simultaneously for reading. However, a user/application that opens a file for writing prevents access to the file as long as it is open. Similarly, if a file is already open for reading, you cannot open it for writing.

3. Logically, you cannot create a file for reading, the file will simply be empty!! Since you can only open a file using FileMode.CreateNew for *writing*, therefore no one else would be able to open the file as long as it is open by your application.

_______________

  Nayer Naguib
0
 

Author Comment

by:jskfan
ID: 16674526
If a user runs the application that has:
 filemode.open, Fileaccess.write

and the file was already open by another user for reading or writing , what would heappen?
0
 
LVL 14

Assisted Solution

by:nayernaguib
nayernaguib earned 1600 total points
ID: 16674794
The application would throw an IOException that contains the following message:

"The process cannot access the file 'xyz.xyz' because it is being used by another process.", where xyz.xyz is the file that was already open.

Therefore you should enclose the above statement within a Try/Catch block, as in the following example:

        Dim fs As System.IO.FileStream
        Try
            fs = New System.IO.FileStream("e:\xyz.txt", IO.FileMode.Open, IO.FileAccess.Write)
        Catch ex As System.IO.IOException
            MessageBox.Show(ex.Message)
        End Try

_______________

  Nayer Naguib
0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is about my first experience with programming Arduino.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Six Sigma Control Plans
Starting up a Project

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question