473,508 Members | 2,400 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with reading a File

37 New Member
Hi All,

I have a problem in my page. i am using below code to read the content of a file.

in the page load method i am writing this code.

Dim sLine() As String = File.ReadAllLines(sLicenseFile)


it works fine when single user access the page.

But problem occurs when another user try to access the page then it kill the prosessing of first use.


any idea why this is happening.

and if i remove this line of code then application works fine. both the user can access the application at the same time.


what else i can do to read the file. i have tried 'Streamreader' also but the same problem.

Dim objSR As StreamReader = New StreamReader(sLicenseFile)
Dim sLine As String = objSR.ReadLine()
objSR.Close()
objSR = Nothing



please shed some light on it

Thanks

-john
Sep 9 '08 #1
5 1092
DonBytes
25 New Member
Been a long time since I did anything in VB but the File class has a method called OpenRead if I recall correctly.

So perhaps something like this would work:
Expand|Select|Wrap|Line Numbers
  1. Using objSR As StreamReader = New StreamReader(File.OpenRead(sLicenseFile))
  2.  
  3. End Using
and then use objSR.ReadToEnd()

Also there is a FileStream class where you can specify FileAccess which might help if the above code doesn't.
Sep 9 '08 #2
joedeene
583 Contributor
Hi All,

I have a problem in my page. i am using below code to read the content of a file.

in the page load method i am writing this code.

Dim sLine() As String = File.ReadAllLines(sLicenseFile)


it works fine when single user access the page.

But problem occurs when another user try to access the page then it kill the prosessing of first use.


any idea why this is happening.

and if i remove this line of code then application works fine. both the user can access the application at the same time.


what else i can do to read the file. i have tried 'Streamreader' also but the same problem.

Dim objSR As StreamReader = New StreamReader(sLicenseFile)
Dim sLine As String = objSR.ReadLine()
objSR.Close()
objSR = Nothing



please shed some light on it

Thanks

-john

hmm, by doing "objSR = Nothing" im wondering if that cancels the "Dim objSR As StreamReader = New StreamReader(sLicenseFile)" maybe? or possibly something when you *REDIM* it as a new streamreader...

joedeene
Sep 9 '08 #3
PRR
750 Recognized Expert Contributor
"I have a problem in my page. i am using below code to read the content of a file.

in the page load method i am writing this code.

Dim sLine() As String = File.ReadAllLines(sLicenseFile)"

as long as readin a file is concerned ... simultaneously read cannot/should not cause prroblems


"it works fine when single user access the page.
But problem occurs when another user try to access the page then it kill the prosessing of first use."


" and if i remove this line of code then application works fine. both the user can access the application at the same time."


what else i can do to read the file. i have tried 'Streamreader' also but the same problem.

Dim objSR As StreamReader = New StreamReader(sLicenseFile)
Dim sLine As String = objSR.ReadLine()
objSR.Close()
objSR = Nothing

// You need not do that .... better way as DonBytes explained use using


Expand|Select|Wrap|Line Numbers
  1. public static void Read()
  2.         {
  3.             //if (b == false)
  4.             //{
  5.             //    Thread.Sleep(1);
  6.             //    b = true;
  7.             //}
  8.  
  9.             string s = string.Empty;
  10.             using (StreamReader sr = new StreamReader(@"C:\1.txt"))
  11.             {
  12.                 while (sr.Peek() >=0)
  13.                 {
  14.                     //s = sr.ReadToEnd();
  15.                     s = sr.ReadLine();
  16.                     Thread.Sleep(10);
  17.                     Console.WriteLine(s);
  18.                 }
  19.             }
  20.  
  21.  
  22.         }
  23.  
  24. //
  25. // main 
  26.  
  27.           Thread t = new Thread(new ThreadStart(Program.Read));
  28.  
  29.             Thread t1 = new Thread(new ThreadStart(Program.Read));
  30.  
  31.             t.Start();
  32.             t1.Start();
  33.  
  34.             Console.ReadLine();
  35.  
  36.  
Reading shouldn't be an issue....
Sep 10 '08 #4
john20
37 New Member
Hi Guys,

Many thnaks for replying. i have tried writing code with 'Using' but same problem.

Dim line As String = String.Empty
Using fs As System.IO.FileStream = New FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read)
Using reader As New StreamReader(fs)
While line IsNot Nothing
line = reader.ReadLine()
If line IsNot Nothing Then
lines.Add(line)
End If
End While
End Using
End Using

Any other suggestion will be appriciated.

Thanks
Sep 10 '08 #5
PRR
750 Recognized Expert Contributor
Expand|Select|Wrap|Line Numbers
  1. public void Read1()// Call with object ... dont use static....
  2.         {
  3. //if u use static these may get affected...
  4.             string path = @"C:\1.txt";
  5.             string result = string.Empty;
  6.             String line = String.Empty;
  7.                 using(System.IO.FileStream fs   = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
  8.                 {
  9.  
  10.                     using(StreamReader reader =new  StreamReader(fs))
  11.                     {
  12.                         //While line IsNot Nothing
  13.  
  14.                         while (reader.Peek() >=0)
  15.                         {
  16.                             line=reader.ReadLine();
  17.                             result += " " + line;// use string.builder
  18.                             Thread.Sleep(100);
  19.                         }       
  20.  
  21.                     }
  22.                 }
  23.                 Console.WriteLine(result);
  24.  
  25.         }
  26.  
  27.  
Sep 10 '08 #6

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

Similar topics

1
7035
by: fabrice | last post by:
Hello, I've got trouble reading a text file (event viewer dump) by using the getline() function... After 200 - 300 lines that are read correctly, it suddenly stops reading the rest of the...
8
9829
by: Brandon McCombs | last post by:
This may be the wrong group but I didn't see anything for VC++ so I'm trying here. I have a C++ book by Deitel and Deitel that says I can use fstream File("data.dat", ios::in | ios::out |...
1
2559
by: Magnus Lycka | last post by:
I'm trying to read standard out in a process started with popen2 in a non-blocking way. (Other good ways of doing this than the one I tried are appreciated.) I've tried to dumb down my code to...
7
5392
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
2
2464
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() { ...
0
3910
by: Lokkju | last post by:
I am pretty much lost here - I am trying to create a managed c++ wrapper for this dll, so that I can use it from c#/vb.net, however, it does not conform to any standard style of coding I have seen....
0
1742
by: Fabrice | last post by:
Hello, (Alain) Tis is a part of my code to retrieve text from hastable in memory cache, by reading (befor) a resources file. Thanks for your help. /1/ The resources file * I have create a...
5
14968
blazedaces
by: blazedaces | last post by:
Ok, so you know my problem, java is running out of memory reading with SAX, the event-based xml parser intended more-so than DOM for extremely large files. I'll try to explain what I've been doing...
6
3511
by: efrenba | last post by:
Hi, I came from delphi world and now I'm doing my first steps in C++. I'm using C++builder because its ide is like delphi although I'm trying to avoid the vcl. I need to insert new features...
10
2181
by: oktayarslan | last post by:
Hi all; I have a problem when inserting an element to a vector. All I want is reading some data from a file and putting them into a vector. But the program is crashing after pushing a data which...
0
7223
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,...
0
7114
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...
0
7377
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...
1
7034
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...
1
5045
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new...
0
4702
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...
0
3179
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1544
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 ...
1
762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.