Leeus Posted September 27, 2003 Posted September 27, 2003 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?? Quote
aewarnick Posted September 28, 2003 Posted September 28, 2003 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; } Quote C#
Administrators PlausiblyDamp Posted September 28, 2003 Administrators Posted September 28, 2003 Notepad opens the file, reads it in and then closes the file straight away - this stops it keeping the file locked. Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leeus Posted September 28, 2003 Author Posted September 28, 2003 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 Quote
aewarnick Posted September 28, 2003 Posted September 28, 2003 Why would you be using notepad to open the file if you can just use your program? Quote C#
Leeus Posted September 28, 2003 Author Posted September 28, 2003 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)) Quote
Administrators PlausiblyDamp Posted September 28, 2003 Administrators Posted September 28, 2003 have you tried Dim sr As New StreamReader(New FileStream(filelocation, FileMode.Open, FileAccess.Read, FileShare.Read )) Quote Posting Guidelines FAQ Post Formatting Intellectuals solve problems; geniuses prevent them. -- Albert Einstein
Leeus Posted September 28, 2003 Author Posted September 28, 2003 Just tried that and still same error! Quote
aewarnick Posted September 28, 2003 Posted September 28, 2003 Do you get that error only when Notepad is open? If so, close the file and try again. Otherwise reboot and try again. Quote C#
Leeus Posted September 28, 2003 Author Posted September 28, 2003 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. Quote
tuningd Posted October 2, 2003 Posted October 2, 2003 I don't know it is possible but could you copy the file and open the copied version that is not being written to? Quote
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.