473,473 Members | 1,552 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

open a streamwriter/reader

134 New Member
I am trying to make a program which acts just a bit like a database. The only problem yet is that I had problems with my streamwriters and readers. It seems that my streamwriter only wants to write a string in a txt file when I say
Expand|Select|Wrap|Line Numbers
  1. sw.Close();
, but how can I open the stream again?
Apr 7 '11 #1
4 3056
GaryTexmo
1,501 Recognized Expert Top Contributor
Only close the stream when you're done with it. That said, if you want to reopen it, just do it the same way you did in the first place.
Apr 7 '11 #2
adriancs
122 New Member
Open, Read and Close a file:
Expand|Select|Wrap|Line Numbers
  1. string FileName = @"C:\myFileName.txt";
  2. string[] stringArray = null;
  3.  
  4. if (File.Exists(FileName))
  5. {
  6.     stringArray = File.ReadAllLines(FileName);
  7.     foreach (string line in stringArray)
  8.     {
  9.         // Do something here...
  10.     }
  11. }
  12. else
  13. {
  14.     MessageBox.Show("File Not Exists");
  15. }
Open, Write and Close a file:
Expand|Select|Wrap|Line Numbers
  1. string filePath = @"C:\";
  2. string fileName = "myFileName.txt";
  3. string[] stringArray = null; // replace this with your data
  4. if (Directory.Exists(filePath))
  5.     File.WriteAllLines(filePath + fileName, stringArray, Encoding.UTF8);
  6. else
  7.     MessageBox.Show("The directory is not exist");
May 1 '11 #3
Plater
7,872 Recognized Expert Expert
If you are only seeing your data written on the .Close() then I would guess you need to call Flush().
You should be able to keep the file open(if that is what you want) and keep writing, calling Flush() when required
May 3 '11 #4
bvrwoo
16 New Member
You must keep the file open while writing because when you close all operations are processed and then the file is close and unmanaged resources are destroyed.
Expand|Select|Wrap|Line Numbers
  1. using (TextWriter tw = new StreamWriter(file))
  2. {
  3.     foreach (string s in strings)
  4.         tw.WriteLine(s);//or whatever you are saving as string
  5. }
  6.  
you don't need to call on tw.Close() because it is in a 'using' block which you can use for any class/struct which inherits the IDisposable interface.
Aug 29 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

6
by: wk6pack | last post by:
Hi, I have a question about my coding practise. I have a class method to return a value from a database. I open the connection do my search and dispose the reader. Open the reader with a new...
2
by: Michael | last post by:
A webpage has the following js: function openPDF(PDFdoc) { // Open a blank window and target PDFdoc into it. // PDFdoc = web address (URL) of the PDF document to present in a pop-up window
4
by: Anand Ganesh | last post by:
Hi All, I want to open a .pdf file from my C# application and show to the user. I have acrobat reader in the client's computer. How to do this? Any suggestions please ? Thanks for your...
6
by: Jimbo | last post by:
After I read or write a file with streamreader and streamwriter and I close it with the Close method, does that automatically let go of the file so that any other process can modify it? In my...
3
by: Craig Lister | last post by:
Newbie alert! I am trying to get data from my mySQL database... With some help, I have got this far, but there is an exception in data.dll (??) on the : "OleDbDataReader Reader =...
10
by: Oscar Thornell | last post by:
Hi, I generate and temporary saves a text file to disk. Later I upload this file to Microsoft MapPoint (not so important). The file needs to be in UTF-8 encoding and I explicitly use the...
1
by: vbDavidC | last post by:
Hi, I am fairly new to .net and objects. I learned to create a reader object in method 1, however if I wanted to create multiple select queries in the same module I did not know how to reuse...
1
by: weird0 | last post by:
The property of GridView1.HasRows is true and after assigning GridView.DataSource=reader, it does not display any records in the GirdView. I am i missing out something? Here is the code:...
1
by: arunairs | last post by:
Hi, After writing the £ (pound sterling) symbol to a file using StreamWriter using the default encoding UTF8, when I retrieve it the file symbol £ gets changed. But when I use Encoding.Default in...
0
by: bill | last post by:
Can someone please show me what to do with this to show the data in a dataGrid for example? I'm not really sure what to do with it. I'm used to Vb6. Dim Con = New...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
0
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.