473,325 Members | 2,342 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,325 software developers and data experts.

safely open file for reading...

!NoItAll
297 100+
I'm confused a bit. I need to open a file that another program regular reads and writes. I want to make sure that I open the program in such a way that it will not harm the other programs access in any way. My program can handle any errors created by the other program changing the file out from under me - I just need to make sure that when I open the file the other program can continue to read, write, delete, etc without even knowing about my program.
FYI this is simply a weather application where the "other" program is updating a table file with current weather data - my program reads that data and sends it to another application...

Here is the code I am using:

Expand|Select|Wrap|Line Numbers
  1. Dim FS As IO.FileStream = Nothing
  2. '... other code here...
  3. FS = New IO.FileStream(sPath, IO.FileMode.Open, IO.FileAccess.Read, IO.FileShare.ReadWrite Or IO.FileShare.Delete Or IO.FileShare.Read Or IO.FileShare.Write)
  4.  
So my confusion is if the flags are granting the stated access to other applications, or if they are taking the access for my program. I only need to read - but somewhere along the line I believe I found that these flags are for the access levels I want to grant to other programs.
Any help would be appreciated!

Des
Aug 2 '10 #1
1 1317
Aimee Bailey
197 Expert 100+
The correct way to do this is as follows...

Expand|Select|Wrap|Line Numbers
  1. Try
  2.     Using fs As New FileStream(filename, FileMode.Open, FileAccess.Read)
  3.         Using sr As New StreamReader(fs)
  4.           'do stuff here
  5.         End Using
  6.     End Using
  7. Catch ex As IOException
  8.     MessageBox.Show(ex.Message & vbCrLf & ex.StackTrace)
  9. End Try
  10.  
When accessing files that other programs access, we need to specify both the mode and access methods so that windows is satisfied that we won't try to modify the stream. Also i always reccomend using's as they make sure to dispose of things properly.

Also, the FileAccess only needs to specify the rights your stream has in particular.

Aimee
Aug 10 '10 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: C Williams | last post by:
Hi. If I have just uploaded an Excel file to my computer from a user, I would like to use code to open it and scan it for some information without risk of infecting my computer with a virus or...
6
by: Charles Morrall | last post by:
I have no experience with DB2 as such, but I've been tasked with configuring backup of a server running DB2 v8 on Windows Server 2003. I do have some experience with backups in general though. The...
2
by: Sabin Finateanu | last post by:
Hi I'm having problem reading a file from my program and I think it's from a procedure I'm using but I don't see where I'm going wrong. Here is the code: public bool AllowUsage() { ...
1
by: archana | last post by:
Hi all, I am new to asp.net. I heard somthing like we can't open file which is there on client pc without uploading that file onto out server. Is it right? If i am wrong then please correct...
1
by: bbepristis | last post by:
Hey all I have a wired issue I have a csv file with , seperated values I have some code to read the file and put the record into an array then into textboxes. the problem is it seems to read...
5
by: Anja | last post by:
Hi everyone, I have a question about text file reading with VBA. I want to read he whole contents of the file in one string variable. I have been able to successfully read lines using: Line...
4
by: DyslexicAnaboko | last post by:
Hello, I have a module that is part of larger project that is giving me trouble, so I setup an example. Brief ===== I simply want to open a text file and make the contents avaliable...
5
by: Ryan Liu | last post by:
Hi, Both way works, I'd just ask some experts which way is better? My application creates a log file daily. Now each time when I write a log, I will open the file and append to the end....
6
by: gonzlobo | last post by:
I've been using Python for a few days. It's such the perfect language for parsing data! I really like it so far, but I'm having a hard time reading a file, reading the first few hex characters &...
2
by: aaron.watters | last post by:
Hi. I'm writing an archival system which I'd like to be portable to Windows. The system relies on the property of Unix which allows a process to keep a file open even if another process renames...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.