File Access

Leeus

Well-known member
Joined
Dec 23, 2002
Messages
50
I am trying to open a txt file in a project using the .openfile command within the IO namespace, this works fine but when I try and use it on a file that is being written to it wont let me!! That is understandable but notepad seems to manage it!! Can I do this by opening it in read only mode or something??
 
You shouldnt have a problem. This works
C#:
//my own messagebox and own Read File method:
a.MB.ShowDialog(a.Files.ReadText("D:\\test.txt"));

//the Read File method in class a, class Files (my own classes):
/// <summary>
			/// Reads a text file from disk.
			/// </summary>
			public static string ReadText(string path)
			{
				string s="";
				Stream F=new FileStream(path, FileMode.Open, FileAccess.Read);
				if(F.Length> long.MaxValue)
				{
					int onebyte= 0;
					int bytesR= 0;
					byte[]c= new Byte[ulong.MaxValue];

					while(true)
					{
						onebyte= F.ReadByte();
						if(onebyte==-1) {F.Close(); s= Encoding.ASCII.GetString(c, 0, c.Length); return s;}
						c[bytesR]= (byte)onebyte; ++bytesR;
					}
				}
					
				if(F.Length> int.MaxValue)
				{
					int onebyte= 0;
					int bytesR= 0;
					byte[]c= new Byte[F.Length];

					while(true)
					{
						onebyte= F.ReadByte();
						if(onebyte==-1) {F.Close(); s= Encoding.ASCII.GetString(c, 0, c.Length); return s;}
						c[bytesR]= (byte)onebyte; ++bytesR;
					}
				}

				byte[]b=new Byte[F.Length];
				F.Read(b, 0, Convert.ToInt32(F.Length));
				s= Encoding.ASCII.GetString(b);
				F.Close();
				return s;
			}
 
The file we are talking about is a 1.3 gig text file, notepad takes an age to open it!
I cant see how to adapt it for my code??

Code:
txtfilename.Text = txttype.Text & txtdate.Text & ".LOG"
            Dim filelocation As String = "c:\mail\Log\" & txtfilename.Text
            Dim sr As New System.IO.StreamReader(filelocation)

And then I have to do a readline to read the text file in, this is within a look until readline = nothing
 
Notepad takes ages but opens the file the VB program gives this error!

"The process cannot access the file "c:\mail\Log\PT030928.LOG" because it is being used by another process." I have adapted the code as follows but still doesnt work!

Code:
            Dim sr As New StreamReader(New FileStream(filelocation, FileMode.Open, FileAccess.Read))
 
Do you get that error only when Notepad is open? If so, close the file and try again. Otherwise reboot and try again.
 
No notepad is not open, the only reason I mentioned it was to prove the file could be opened.

The file is a log file constantly being written to.
 
Back
Top