|
web
newsgroups
|
|||||||||||||||||||||||
|
|||||||||||||||||||||||
Text Filessame file for reading (line by line). I've so far been able to successfully open the file, write to it, then close it. I let the user decide which file to write to, using the following code: ' File writing/reading variables Dim SaveFileResult As SaveFileDialog Dim TextFileStream As FileStream Dim TextStreamWriter As StreamWriter Dim TextStreamReader As StreamReader SaveFileResult = New SaveFileDialog SaveFileResult.Filter = "Text Files|*.txt|All Files|*.*" SaveFileResult.FilterIndex = 0 Dim FileResult As DialogResult FileResult = SaveFileResult.ShowDialog If FileResult = DialogResult.OK Then ' Open file for writing TextFileStream = SaveFileResult.OpenFile TextStreamWriter = New StreamWriter(TextFileStream) Else ' Since they cancelled, exit sub txtText.Focus() txtText.Select(0, txtText.Text.Length) Exit Sub End If End If .... This works successfully. Is there a way I can then change it from write-only access to read-only access, or would I need to close it first? Also, how can I open it with the same filename? I'm really new to working with files, so any help would be appreciated. Thank you very much! Eric,
The samples at MSDN about the streamwriter are in my idea a lot different what you use, while for your question in my idea they are given. http://msdn.microsoft.com/library/default.asp?url=/library/en-us/cpref/html/frlrfsystemiostreamreaderclasstopic.asp You write about the close, however I see it nowhere in your code while it is really two times needed. As extra if you want a quick answer, than please make the code about the problem and not about the OpenFileDialog and the SaveFileDialog as it is now. Try than to avoid blank lines in your messages, this you can get using the nobebook or using this program from Armin. http://www.vb-tips.com/default.aspx?ID=10a3afac-92e3-4ea1-ad59-0487108f3513 I hope this helps, Cor Hi Eric,
The way I open files using a OpenFileDialog is: 1. Store the path to the file using the FileName property of the FileDialog. 2. Use one of the overloaded constructors of the FileStream class to create a new filestream: Dim TextFileStream As New FileStream(strFileName, FileMode.Open, FileAccess.Read, FileShare.None) The FileAccess and FileShare enumerations will help you to set Readonly or Writeonly access. 3. Create a StreamReader or Writer which will then read or write to your selected file. You can open the file with the same file name since the path to the file would be stored in your variable. Hope that helps, Regards, Cerebrus. Show quoteHide quote "Eric A. Johnson" <noth***@dontlookforme.com> wrote in message news:HI8Kf.10281$rL5.9417@newssvr27.news.prodigy.net... > I'm trying to create a text file, write to it, close it, then reopen the > same file for reading (line by line). I've so far been able to successfully > open the file, write to it, then close it. I let the user decide which file > to write to, using the following code: > ' File writing/reading variables > > Dim SaveFileResult As SaveFileDialog > > Dim TextFileStream As FileStream > > Dim TextStreamWriter As StreamWriter > > Dim TextStreamReader As StreamReader > > SaveFileResult = New SaveFileDialog > > SaveFileResult.Filter = "Text Files|*.txt|All Files|*.*" > > SaveFileResult.FilterIndex = 0 > > Dim FileResult As DialogResult > > FileResult = SaveFileResult.ShowDialog > > If FileResult = DialogResult.OK Then > > ' Open file for writing > > TextFileStream = SaveFileResult.OpenFile > > TextStreamWriter = New StreamWriter(TextFileStream) > > Else ' Since they cancelled, exit sub > > txtText.Focus() > > txtText.Select(0, txtText.Text.Length) > > Exit Sub > > End If > > End If > > > > ... This works successfully. Is there a way I can then change it from > write-only access to read-only access, or would I need to close it first? > Also, how can I open it with the same filename? I'm really new to working > with files, so any help would be appreciated. Thank you very much! > > Cerebrus.
Does still not work if you don't close the file after writing before you start to read it. The filedialog is not the qeustion in my opinion. just a way to find the path. Cor Hi Cor,
I know that, of course, but in that case I must have misunderstood Eric's question. I assumed that the FileStream would be closed before attempting another read/write. The way I saw it, he was creating his FileStream using the "TextFileStream = SaveFileResult.OpenFile" statement, where SaveFileResult is the SaveFileDialog. Using this option, one has to go with the default options and is limited in flexibility as far as Read/Write access control is concerned. To quote the Help : SaveFileDialog.OpenFile Method : ------------------------------------ "For security purposes, this method creates a new file with the selected name and opens it with read/write permissions. This can cause unintentional loss of data if you select an existing file to save to. To save data to an existing file while retaining existing data, use the File class to open the file using the file name returned in the FileName property." That was what I wanted to explain in my post. Regards, Cerebrus. Show quoteHide quote "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message news:uLNmUohNGHA.3576@TK2MSFTNGP15.phx.gbl... > Cerebrus. > > Does still not work if you don't close the file after writing before you > start to read it. > > The filedialog is not the qeustion in my opinion. just a way to find the > path. > > Cor > > Thank you both for your replies! I truly appreciate it a lot. You've been
a terrific help to me! :) -- Eric Show quoteHide quote "Cerebrus99" <zorg***@sify.com> wrote in message news:euJCtuhNGHA.536@TK2MSFTNGP09.phx.gbl... > Hi Cor, > > I know that, of course, but in that case I must have misunderstood Eric's > question. I assumed that the FileStream would be closed before attempting > another read/write. > > The way I saw it, he was creating his FileStream using the > "TextFileStream = SaveFileResult.OpenFile" statement, where SaveFileResult > is the SaveFileDialog. > > Using this option, one has to go with the default options and is limited > in > flexibility as far as Read/Write access control is concerned. > > To quote the Help : > > SaveFileDialog.OpenFile Method : > ------------------------------------ > "For security purposes, this method creates a new file with the selected > name and opens it with read/write permissions. This can cause > unintentional > loss of data if you select an existing file to save to. To save data to an > existing file while retaining existing data, use the File class to open > the > file using the file name returned in the FileName property." > > That was what I wanted to explain in my post. > > Regards, > > Cerebrus. > > > "Cor Ligthert [MVP]" <notmyfirstn***@planet.nl> wrote in message > news:uLNmUohNGHA.3576@TK2MSFTNGP15.phx.gbl... >> Cerebrus. >> >> Does still not work if you don't close the file after writing before you >> start to read it. >> >> The filedialog is not the qeustion in my opinion. just a way to find the >> path. >> >> Cor >> >> > >
Help creating custom ComboBox control
Can opacity/transparency be adjusted within components? Extract image dimensions (height, width) from Base64 String? DrawIconEx No tables visible in SQL Server connection Progress bar - No activity Basic How To Question: Class Library or Windows Control Library System tray icon Basic DirectSound question PropertyChanged event with VB.NET (.NET 1.1) |
|||||||||||||||||||||||