473,568 Members | 2,935 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Disappearing data from StreamWriter

I'm having a problem with a very basic program. I'm creating a app in
Visual Studio (2003) C# to zip up a collection of specified files &
directories and save them to a certain location.

I have a checkedListBox that holds all the directories and files that I
want saved. I have two buttons (One for selecting file & one for
directory) that are hooked to a openFileDialog and folderBrowserDi alog.
When a new file or dir is selected, it is written to an arrayList
backupLocations , then displayed in the checkedListBox, then the
contents of backupLocations are written to a text file so the state can
be restored if the app is closed.

The problem is with the files. Adding directories get stored fine.
But for some reason if you add a file, it will not save the state
properly. I can single step through the code and see the string name
of the file in the char buffer of the StreamWriter but for some reason
it will not be written to the file. I'm totally stumped here.

Here are the four specific functions:

private void button2_Click(o bject sender, System.EventArg s e){
//Adds the selected FILE location to the backupLocations arraylist.
if (openFileDialog 1.ShowDialog() == DialogResult.OK )
addLocation(ope nFileDialog1.Fi leName); }

private void button3_Click(o bject sender, System.EventArg s e){
//Adds the selected DIRECTORY location to the backupLocations
arraylist.
if (folderBrowserD ialog1.ShowDial og() == DialogResult.OK )
addLocation(fol derBrowserDialo g1.SelectedPath );}

public void addLocation(str ing location){
backupLocations .Add(location);
checkedListBox1 .Items.Clear();
checkedListBox1 .Items.AddRange (
backupLocations .ToArray(typeof (string)) as string[] );

saveState();}
public void saveState(){
using(StreamWri ter writer = File.CreateText (filePath)) {
foreach(string backupLocation in backupLocations )
writer.WriteLin e(backupLocatio n);
writer.Close();
}
}

For example, I can set a breakpoint on the
writer.WriteLin e(backupLocatio n) and see the data in the
streamWriter--but it will not show up in the text file. I can have two
directories then a file, but the file will not be written but the
directories will.

Is there something different about the type of string that comes from
openFileDialog1 .FileName?

I would be thankful for any type of help
Regards,
NoTalent

I have attached the full code for the sample app I wrote for debugging
this:

//*************** *************CO DE************* *************** *****//
using System;
using System.Drawing;
using System.Collecti ons;
using System.Componen tModel;
using System.Windows. Forms;
using System.Data;

using System.IO;

namespace StateTest
{
/// <summary>
/// Summary description for Form1.
/// </summary>
public class Form1 : System.Windows. Forms.Form
{
private System.Windows. Forms.Button button1;
private System.Windows. Forms.Button button2;
private System.Windows. Forms.Button button3;
private System.Windows. Forms.TextBox textBox1;
private System.Windows. Forms.OpenFileD ialog openFileDialog1 ;
private System.Windows. Forms.FolderBro wserDialog
folderBrowserDi alog1;
private System.Windows. Forms.CheckedLi stBox checkedListBox1 ;
/// <summary>
/// Required designer variable.
/// </summary>
private System.Componen tModel.Containe r components = null;

private ArrayList backupLocations ;
private System.Windows. Forms.Button button4;
private string filePath;

public Form1()
{
//
// Required for Windows Form Designer support
//
InitializeCompo nent();

backupLocations = new ArrayList();
filePath = "state.dat" ;
restoreState();

}

/// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Disp ose();
}
}
base.Dispose( disposing );
}

#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeCompo nent()
{
this.button1 = new System.Windows. Forms.Button();
this.button2 = new System.Windows. Forms.Button();
this.button3 = new System.Windows. Forms.Button();
this.textBox1 = new System.Windows. Forms.TextBox() ;
this.openFileDi alog1 = new System.Windows. Forms.OpenFileD ialog();
this.folderBrow serDialog1 = new
System.Windows. Forms.FolderBro wserDialog();
this.checkedLis tBox1 = new System.Windows. Forms.CheckedLi stBox();
this.button4 = new System.Windows. Forms.Button();
this.SuspendLay out();
//
// button1
//
this.button1.Lo cation = new System.Drawing. Point(8, 8);
this.button1.Na me = "button1";
this.button1.Ta bIndex = 0;
this.button1.Te xt = "Add Text";
this.button1.Cl ick += new System.EventHan dler(this.butto n1_Click);
//
// button2
//
this.button2.Lo cation = new System.Drawing. Point(8, 40);
this.button2.Na me = "button2";
this.button2.Ta bIndex = 1;
this.button2.Te xt = "Add File";
this.button2.Cl ick += new System.EventHan dler(this.butto n2_Click);
//
// button3
//
this.button3.Lo cation = new System.Drawing. Point(8, 72);
this.button3.Na me = "button3";
this.button3.Ta bIndex = 2;
this.button3.Te xt = "Add Dir";
this.button3.Cl ick += new System.EventHan dler(this.butto n3_Click);
//
// textBox1
//
this.textBox1.L ocation = new System.Drawing. Point(96, 8);
this.textBox1.N ame = "textBox1";
this.textBox1.S ize = new System.Drawing. Size(328, 20);
this.textBox1.T abIndex = 3;
this.textBox1.T ext = "";
//
// checkedListBox1
//
this.checkedLis tBox1.CheckOnCl ick = true;
this.checkedLis tBox1.Horizonta lScrollbar = true;
this.checkedLis tBox1.Location = new System.Drawing. Point(96, 40);
this.checkedLis tBox1.Name = "checkedListBox 1";
this.checkedLis tBox1.Size = new System.Drawing. Size(328, 124);
this.checkedLis tBox1.TabIndex = 4;
//
// button4
//
this.button4.Lo cation = new System.Drawing. Point(8, 104);
this.button4.Na me = "button4";
this.button4.Ta bIndex = 5;
this.button4.Te xt = "Remove";
this.button4.Cl ick += new System.EventHan dler(this.butto n4_Click);
//
// Form1
//
this.AutoScaleB aseSize = new System.Drawing. Size(5, 13);
this.ClientSize = new System.Drawing. Size(432, 173);
this.Controls.A dd(this.button4 );
this.Controls.A dd(this.checked ListBox1);
this.Controls.A dd(this.textBox 1);
this.Controls.A dd(this.button3 );
this.Controls.A dd(this.button2 );
this.Controls.A dd(this.button1 );
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayo ut(false);

}
#endregion

/// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
{
Application.Run (new Form1());
}

public void saveState()
{
using(StreamWri ter writer = File.CreateText (filePath))
{
foreach(string backupLocation in backupLocations )
writer.WriteLin e(backupLocatio n);

writer.Close();
}
}

public void restoreState()
{
//If file exists, restore settings
if(File.Exists( filePath))
{
StreamReader sr = File.OpenText(f ilePath);
String input;

while ((input=sr.Read Line())!=null)
{
backupLocations .Add(input);
}
checkedListBox1 .Items.AddRange (
backupLocations .ToArray(typeof (string)) as string[] );
sr.Close();
}
}

//It clears the checkedListBox and adds the backupLocations arrayList
items to the checkedListBox
//Then saves the state of the app
public void addLocation(str ing location)
{
backupLocations .Add(location);
checkedListBox1 .Items.Clear();
checkedListBox1 .Items.AddRange (
backupLocations .ToArray(typeof (string)) as string[] );

saveState();
}
private void button1_Click(o bject sender, System.EventArg s e)
{
//Adds the selected TEXT in textbox 1
if( (textBox1.Text != "") || (textBox1.Text != null) )
addLocation(tex tBox1.Text);
}

private void button2_Click(o bject sender, System.EventArg s e)
{
//Adds the selected FILE location to the backupLocations arraylist.

if (openFileDialog 1.ShowDialog() == DialogResult.OK )
addLocation(ope nFileDialog1.Fi leName);
}

private void button3_Click(o bject sender, System.EventArg s e)
{
//Adds the selected DIRECTORY location to the backupLocations
arraylist.
if (folderBrowserD ialog1.ShowDial og() == DialogResult.OK )
addLocation(fol derBrowserDialo g1.SelectedPath );
}

private void button4_Click(o bject sender, System.EventArg s e)
{
//Removes checked box
for (int i=checkedListBo x1.CheckedIndic es.Count-1; i>=0; i--)
{
backupLocations .RemoveAt(check edListBox1.Chec kedIndices[i]);
}
checkedListBox1 .Items.Clear();
checkedListBox1 .Items.AddRange (
backupLocations .ToArray(typeof (string)) as string[] );

saveState();
}
}
}
//*************** ***********ENDC ODE************ *************** *****//

Jul 21 '05 #1
0 1504

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
1800
by: Daniel | last post by:
i would like to konw when the data sent so that i can close the streamwriter and networkstream is there some sort of call backs/events i have to implement for this to work? if so how? can i just open neworkstream and streamwriter, send data and then close it syncrhronously or do i have to implement some callbacks/events to do this like in vb6?...
1
3778
by: Daniel | last post by:
after opening socket, sending data then closing socket 3000 times i get "Only one usage of each socket address" what am i doing wrong? is there some thing else i need to do to free up the socket after i send data into it? I simply want to open socket, send data, close socket and have the server just handle one client thread to recieve...
2
4079
by: neptune | last post by:
I built a form to access a query with a 2 field primary key. It should use 2 controls to find the unique record and display the other field values on the form. In the criteria section of the query, I set the 2 primary key values equal to the 2 control values on my form. When I input a value to these 2 controls the query finds the correct...
3
2489
by: pei_world | last post by:
hi I am new to C# programming. can anyone tell me what is the standard way to store high sensitive user data for application, so that application next run can get back those data.
2
2601
by: Rachel Suddeth | last post by:
Here is my scenario: I have a few custom controls that I set up on a form and tested setting properties and appearances. Then I added a couple references to the project which add classes I need to get data from the server to actually do something useful. (These are generated by 3rd party database software.) After adding those references,...
6
1622
by: Winshent | last post by:
I have read many threads which indicate that this was a problem with version 2002. Why should i be suffering this? I am using VB.NET 2003 Standard Edition... is it still a problem with 2003? I have used the wizards to generate the connection and adapter, which generates the code in 'InitializeComponent', and i have stored the...
0
267
by: notalent | last post by:
I'm having a problem with a very basic program. I'm creating a app in Visual Studio (2003) C# to zip up a collection of specified files & directories and save them to a certain location. I have a checkedListBox that holds all the directories and files that I want saved. I have two buttons (One for selecting file & one for directory) that...
2
5415
by: Kevien Lee | last post by:
Hi , I had a strang problam ,when i use StreamWriter to append to a file,i found that if i don't close the StreamReader it couldn't write the data into file,the code as folllow that: class Program { static void Main(string args) { FileInfo file = new FileInfo(@"E:\Demo\Log\20061214.log");
4
6211
by: floppyzedolfin | last post by:
Hello! I'm actually encoding an encryption / decryption program. The encryption programes takes a file path in parameter, and encrypts the contents of the file and stores that into another file. I'm using AES for it is quick, and RSA to encrypt AES, to transmit AES keys (it'll run on two separate computers). Please notice that - this is...
0
7693
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7605
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8118
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7665
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
3651
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3631
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2105
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 we have to send another system
1
1207
muto222
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.