Jump to content
Xtreme .Net Talk

Recommended Posts

Posted
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 won't let me!! That is understandable but notepad seems to manage it!! Can I do this by opening it in read only mode or something??
Posted

You shouldn't have a problem. This works

//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;
		}

C#
Posted

The file we are talking about is a 1.3 gig text file, notepad takes an age to open it!

I can't see how to adapt it for my 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

Posted

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 doesn't work!

 

           Dim sr As New StreamReader(New FileStream(filelocation, FileMode.Open, FileAccess.Read))

Posted

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.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...