.NET Forum / Windows Forms / WinForm General / August 2006
cache problem for StreamReader
|
|
Thread rating:  |
Pony Tsui - 24 Aug 2006 11:19 GMT Hi All,
I create a file, use StreamReader to read this file, the file content is corrent, i create this file again with different content, then read this file again, the file content not change, it's seem cache by .Net or FileSystem.
can i solve this problem?
 Signature Pony Tsui
Markus - 24 Aug 2006 11:27 GMT > I create a file, use StreamReader to read this file, the file content > is corrent, i create this file again with different content, then > read this file again, the file content not change, it's seem cache by > .Net or FileSystem. > > can i solve this problem? Be sure, that you close or flush the stream-writer, that is writing the file, otherwise the content may not be written then. Additionally, be sure, that you are reading really the same file again. To simple test it, make a breakpoint in the debugger after writing the file, check the files content, then start over with reading the file...
Maybe you must re-create the reader at the second read - but I don't know that yet, just give it a try, if the above suggestions do not work.
hth Markus
Pony Tsui - 24 Aug 2006 12:13 GMT Hi Markus,
Thanks for your reply.
The file was create by cobol, so i can not flush it, i sure it read the same file because if i read this file about one minites later after first read, the content will be corrent!
 Signature Pony Tsui
> > I create a file, use StreamReader to read this file, the file content > > is corrent, i create this file again with different content, then [quoted text clipped - 14 lines] > hth > Markus Markus - 24 Aug 2006 13:02 GMT > The file was create by cobol, so i can not flush it, i sure it read > the same file because if i read this file about one minites later > after first read, the content will be corrent! Then maybe it's your cobol-application, which does not flush the content and close the file stream correctly... did you have a look at that? Because your problem is, that your file is not immediately available for a second read - therefore the problem must be somewhere in the write process (as I assume from my knowledge of you application).
Markus
Linda Liu [MSFT] - 25 Aug 2006 03:21 GMT Hi Pony,
There's no cache for StreamReader. As Markus and Herfried said, you should make it sure that the new content gets written to the file and use a new StreamReader object to read the file.
Hope this helps.
Sincerely, Linda Liu Microsoft Online Community Support
================================================== Get notification to my posts through email? Please refer to http://msdn.microsoft.com/subscriptions/managednewsgroups/default.aspx#notif ications. Note: The MSDN Managed Newsgroup support offering is for non-urgent issues where an initial response from the community or a Microsoft Support Engineer within 1 business day is acceptable. Please note that each follow up response may take approximately 2 business days as the support professional working with you may need further investigation to reach the most efficient resolution. The offering is not appropriate for situations that require urgent, real-time or phone-based interactions or complex project analysis and dump analysis issues. Issues of this nature are best handled working with a dedicated Microsoft Support Engineer by contacting Microsoft Customer Support Services (CSS) at http://msdn.microsoft.com/subscriptions/support/default.aspx. ================================================== This posting is provided "AS IS" with no warranties, and confers no rights.
Pony Tsui - 25 Aug 2006 05:08 GMT Hi Linda,
Thanks for your reply.
This is my code:
private sub readTestFile()
' call cobol program form to Create a file TEST.TXT ' call cobol program code here
Dim f As StreamReader = Nothing Dim strAllLine As String = "" Dim remoteFileName As String = "remoteServer:\folder\TEST.TXT" Dim localFileName As String = "c:\TEST.TXT." & Format(Now, "HHmmssFF") FileCopy(remoteFileName , localFileName) Try f = New StreamReader(localFileName, Encoding.GetEncoding(950)) Try Do While f.Peek >= 0 strAllLine += Trim(f.ReadLine) Loop Catch ex As Exception MsgBox("Read TEST.TXT Error: " & ex.Message) Finally f.Close() Kill(localFileName) ' if add sleep(5000) i can get the new file content ' System.Threading.Thread.Sleep(5000)
End Try msgbox(strAllLine) end sub
any suggest?
 Signature Pony Tsui
Markus - 25 Aug 2006 07:05 GMT Difficult to say from here, as you code even does not compile (missing end try statement)... and what are you doing in Kill(localFileName)?
However, from the parts I am seeing here, the place where you add the Thread.Sleep is not relevant, as the Sleep is AFTER reading the file. So I suspect that you are changing the variable with the content (strAllLine) in some parts of the code that is not shown here...
Markus
Pony Tsui - 25 Aug 2006 10:37 GMT Thanks Markus, i did not post the full code.
 Signature Pony Tsui
> Difficult to say from here, as you code even does not compile (missing > end try statement)... and what are you doing in Kill(localFileName)? [quoted text clipped - 5 lines] > > Markus Linda Liu [MSFT] - 25 Aug 2006 07:55 GMT Hi Pony,
Thank you for prompt response.
Do you call the cobol program to create a file TEST.TXT on the remote server and then copy it to a local file in the readTestFile method? Is the call to the cobol program asynchronous?
I think you may set a break point right after the code of "FileCopy(remoteFileName , localFileName)" to see if the new content is copied to the local file.
Hope this helps.
Sincerely, Linda Liu Microsoft Online Community Support
Pony Tsui - 26 Aug 2006 12:42 GMT Hi Linda,
Thanks for your reply.
My cobol program create a file and given the smae file name on a NAS server, then i copy the file from NAS server to local disk given different name, after i call the cobol program twice, the NAS file changed, but the second local file not changed, so maybe it cache by NAS server or cache by the windows file system, the nas server do not have any configure of the cache, is there any way to check the windows file system?
However, because the file created on a unix server, so i change the copy mothed from filecopy to rcp, it's seem work fine.
Thanks Linda, and thanks Markus, you do help me!!
 Signature Pony Tsui
Linda Liu [MSFT] - 29 Aug 2006 05:27 GMT Hi Pony,
> after i call the cobol program twice, the NAS file changed, but the second local file not changed....
Could you tell me what you mean by "the NAS file changed, but the second local file not changed"? Do you call the cobol program twice in the readTestFile procedure? If yes, why do you do like that?
> the nas server do not have any configure of the cache, is there any way to check the windows file system?
Since you just copy the TEST.TXT file on the NAS server to your local machine, I don't think the TEST.TXT file will be cached in your local machine.
> However, because the file created on a unix server, so i change the copy mothed from filecopy to rcp, it's seem work fine.
Do you mean that after you change the copy method from filecopy to rcp, the problem is resolved?
I look forward to your reply.
Sincerely, Linda Liu Microsoft Online Community Support
Herfried K. Wagner [MVP] - 24 Aug 2006 12:06 GMT "Pony Tsui" <PonyTsui@community.nospam> schrieb:
> I create a file, use StreamReader to read this file, the file content is > corrent, > i create this file again with different content, then read this file > again, > the file content not change, it's seem cache by .Net or FileSystem. You'll make sure the data gets written to the file. To do so, call the writer's 'Flush' method. In addition make sure you are using another 'StreamReader' object to read the file.
 Signature M S Herfried K. Wagner M V P <URL:http://dotnet.mvps.org/> V B <URL:http://dotnet.mvps.org/dotnet/faqs/>
Free MagazinesGet these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...
|
|
|