473,625 Members | 3,222 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Read a file that is being used by another process.

I am trying to read a log file using vb.net. I get an error stating
"The process cannot access the file 'C:\test.log' because it is being
used by another process." Below is a sample of what I am trying to do.
The var logFileName is being passed into the function.

Dim line as String
Dim sr As New StreamReader(lo gFileName) 'It dies on this line

Do
line = sr.ReadLine()
Loop Until line is Nothing

sr.Close()

I am new to dot net and not sure if there is another class I should be
using other than the StreamReader class. I just want to read the file
line by line nothing more. Any help would be great. Thanks

Nov 21 '05 #1
4 27105
ng
If the other process has opened the file with exclusive rights, meaning
it is locked to all other processes, then you can't read it. If the
other process opens it as a shared file, then you should be able to also
open and read the file. If you have control over the other process,
then change the way it is opening the file.

Tom
funkmusha wrote:
I am trying to read a log file using vb.net. I get an error stating
"The process cannot access the file 'C:\test.log' because it is being
used by another process." Below is a sample of what I am trying to do.
The var logFileName is being passed into the function.

Dim line as String
Dim sr As New StreamReader(lo gFileName) 'It dies on this line

Do
line = sr.ReadLine()
Loop Until line is Nothing

sr.Close()

I am new to dot net and not sure if there is another class I should be
using other than the StreamReader class. I just want to read the file
line by line nothing more. Any help would be great. Thanks

Nov 21 '05 #2
"funkmusha" <fu*******@hotm ail.com> schrieb:
I am trying to read a log file using vb.net. I get an error stating
"The process cannot access the file 'C:\test.log' because it is being
used by another process." Below is a sample of what I am trying to do.
The var logFileName is being passed into the function.

Dim line as String
Dim sr As New StreamReader(lo gFileName) 'It dies on this line


Try to open the file with 'FileShare.Read Write', which means that other
processes are still allowed to read/write the file:

\\\
Dim fs As New FileStream( _
"C:\test.tx t", _
FileMode.Open, _
FileAccess.Writ e, _
FileShare.ReadW rite _
)
Dim sr As New StreamReader(fs )
....
sr.Close()
///

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #3

Herfried K. Wagner [MVP] wrote:

Try to open the file with 'FileShare.Read Write', which means that other
processes are still allowed to read/write the file:

\\\
Dim fs As New FileStream( _
"C:\test.tx t", _
FileMode.Open, _
FileAccess.Writ e, _
FileShare.ReadW rite _
)
Dim sr As New StreamReader(fs )
...
sr.Close()


If he is only going to read the file, then perhaps FileAccess.Read
would be a better choice than FileAccess.Writ e.

Nov 21 '05 #4
Chris,

"Chris Dunaway" <du******@gmail .com> schrieb:
Try to open the file with 'FileShare.Read Write', which means that other
processes are still allowed to read/write the file:

\\\
Dim fs As New FileStream( _
"C:\test.tx t", _
FileMode.Open, _
FileAccess.Writ e, _
FileShare.ReadW rite _
)
Dim sr As New StreamReader(fs )
...
sr.Close()


If he is only going to read the file, then perhaps FileAccess.Read
would be a better choice than FileAccess.Writ e.


Thank you for making me aware of that. I wrote the sample for
'StreamWriter' and then found out that the OP used a 'StreamReader'. I
simply forgot to change the 'FileAccess' to 'Read'.

--
M S Herfried K. Wagner
M V P <URL:http://dotnet.mvps.org/>
V B <URL:http://classicvb.org/petition/>

Nov 21 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
6790
by: Otik | last post by:
Hi, I'm trying to open a file with read access only, e.g. FileStream fs = new FileStream("C:\foo.txt", FileMode.Open, FileAccess.Read); The file is open by another process and the FileStream constructor throws an IOException. I thought that it was possible to open a file with read access even if it's being used by another process. Is that not true? Thanks, Otik
3
34509
by: trellow | last post by:
Hello, I am writing an application that needs to read a file that is already open by another process for writing. When I do the following: FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read); BinaryReader br = new BinaryReader(fs); I get the following exception:
0
896
by: Binary Poet | last post by:
I am having a problem with a web service that needs to read a file on another server. Is there a way to have the web service run as another user? For example I am developing this web service on my XP Pro workstation. When I do a : If File.Exists(UNC_PATH_TO_FILE) it fails, but if I copy the contents of the UNC_PATH_TO_FILE into explorer on the same computer (XP Pro workstation) it opens the file just fine. So I believe it has to be a...
13
11125
by: George | last post by:
Hi, I am re-writing part of my application using C#. This application starts another process which execute a "legacy" program. This legacy program writes to a log file and before it ends, it writes a specific string to the log file. My original program (MKS Toolkit shell program) which keeps running "grep" checking the "exit string" on the "log files". There are no file sharing problem.
0
2953
nightangel
by: nightangel | last post by:
Hi dude,what i was done in my application is uploading a image file to my server using FTP, it work great when pushing a file into the server path using FTP. The problem i met now is i need to do a resuming for the file uploading if the client connection or server connection is down. E.g: Sunset.jpg (500KB) when i upload until half of it, (332KB in the server) and unplug my connection, it will looping and try to connect to the server, after...
6
16957
by: Studlyami | last post by:
Is it possible to open a file for reading, while another process has it open for writing to it? One of the problems is that the process that is writing to it is written in C and is running on a vxworks OS. I'm reading the file using MFC on a windows XP PC. The file is stored on a Windows 2000 server. I tried using CStdioFile.Open using the CFile::modeRead flag. When did this i received a CFileException with the cause stating...
2
1574
by: Newbie | last post by:
Hi, I have a situation where my application is trying to access a file, that another application may have temporalily open. The error I get is The process cannot access the file 'C:\picks\test50k.gp1' because it is being used by another process.
0
1847
by: Tim Golden | last post by:
aditya shukla wrote: Why on earth are you *specifying* c:\python25 as the directory for this file? It's automatically created in your user-specific temp directory which has specific permissions and is (obviously) specific to that user. You may -- depending on your needs -- also not need the suffix & prefix. ie, unless you have some specific need in mind, just use:
3
2221
by: =?Utf-8?B?RnJhbmsgVXJheQ==?= | last post by:
Hi all I am trying to loop a Textfile. This Textfile is located on a shared drive and it is used by another process. When I try to read it I get an exeption: The process cannot access the file 'file.log' because it is being used by another process.
0
8256
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8189
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8635
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8356
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
5570
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4089
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4193
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2621
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1500
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.