472,811 Members | 1,102 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,811 software developers and data experts.

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 folderBrowserDialog.
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(object sender, System.EventArgs e){
//Adds the selected FILE location to the backupLocations arraylist.
if (openFileDialog1.ShowDialog() == DialogResult.OK)
addLocation(openFileDialog1.FileName); }

private void button3_Click(object sender, System.EventArgs e){
//Adds the selected DIRECTORY location to the backupLocations
arraylist.
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
addLocation(folderBrowserDialog1.SelectedPath);}

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

saveState();}
public void saveState(){
using(StreamWriter writer = File.CreateText(filePath)) {
foreach(string backupLocation in backupLocations)
writer.WriteLine(backupLocation);
writer.Close();
}
}

For example, I can set a breakpoint on the
writer.WriteLine(backupLocation) 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:

//****************************CODE****************** ***************//
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
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.OpenFileDialog openFileDialog1;
private System.Windows.Forms.FolderBrowserDialog
folderBrowserDialog1;
private System.Windows.Forms.CheckedListBox checkedListBox1;
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;

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

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

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.Dispose();
}
}
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 InitializeComponent()
{
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.openFileDialog1 = new System.Windows.Forms.OpenFileDialog();
this.folderBrowserDialog1 = new
System.Windows.Forms.FolderBrowserDialog();
this.checkedListBox1 = new System.Windows.Forms.CheckedListBox();
this.button4 = new System.Windows.Forms.Button();
this.SuspendLayout();
//
// button1
//
this.button1.Location = new System.Drawing.Point(8, 8);
this.button1.Name = "button1";
this.button1.TabIndex = 0;
this.button1.Text = "Add Text";
this.button1.Click += new System.EventHandler(this.button1_Click);
//
// button2
//
this.button2.Location = new System.Drawing.Point(8, 40);
this.button2.Name = "button2";
this.button2.TabIndex = 1;
this.button2.Text = "Add File";
this.button2.Click += new System.EventHandler(this.button2_Click);
//
// button3
//
this.button3.Location = new System.Drawing.Point(8, 72);
this.button3.Name = "button3";
this.button3.TabIndex = 2;
this.button3.Text = "Add Dir";
this.button3.Click += new System.EventHandler(this.button3_Click);
//
// textBox1
//
this.textBox1.Location = new System.Drawing.Point(96, 8);
this.textBox1.Name = "textBox1";
this.textBox1.Size = new System.Drawing.Size(328, 20);
this.textBox1.TabIndex = 3;
this.textBox1.Text = "";
//
// checkedListBox1
//
this.checkedListBox1.CheckOnClick = true;
this.checkedListBox1.HorizontalScrollbar = true;
this.checkedListBox1.Location = new System.Drawing.Point(96, 40);
this.checkedListBox1.Name = "checkedListBox1";
this.checkedListBox1.Size = new System.Drawing.Size(328, 124);
this.checkedListBox1.TabIndex = 4;
//
// button4
//
this.button4.Location = new System.Drawing.Point(8, 104);
this.button4.Name = "button4";
this.button4.TabIndex = 5;
this.button4.Text = "Remove";
this.button4.Click += new System.EventHandler(this.button4_Click);
//
// Form1
//
this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
this.ClientSize = new System.Drawing.Size(432, 173);
this.Controls.Add(this.button4);
this.Controls.Add(this.checkedListBox1);
this.Controls.Add(this.textBox1);
this.Controls.Add(this.button3);
this.Controls.Add(this.button2);
this.Controls.Add(this.button1);
this.Name = "Form1";
this.Text = "Form1";
this.ResumeLayout(false);

}
#endregion

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

public void saveState()
{
using(StreamWriter writer = File.CreateText(filePath))
{
foreach(string backupLocation in backupLocations)
writer.WriteLine(backupLocation);

writer.Close();
}
}

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

while ((input=sr.ReadLine())!=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(string location)
{
backupLocations.Add(location);
checkedListBox1.Items.Clear();
checkedListBox1.Items.AddRange(
backupLocations.ToArray(typeof(string)) as string[] );

saveState();
}
private void button1_Click(object sender, System.EventArgs e)
{
//Adds the selected TEXT in textbox 1
if( (textBox1.Text != "") || (textBox1.Text != null) )
addLocation(textBox1.Text);
}

private void button2_Click(object sender, System.EventArgs e)
{
//Adds the selected FILE location to the backupLocations arraylist.

if (openFileDialog1.ShowDialog() == DialogResult.OK)
addLocation(openFileDialog1.FileName);
}

private void button3_Click(object sender, System.EventArgs e)
{
//Adds the selected DIRECTORY location to the backupLocations
arraylist.
if (folderBrowserDialog1.ShowDialog() == DialogResult.OK)
addLocation(folderBrowserDialog1.SelectedPath);
}

private void button4_Click(object sender, System.EventArgs e)
{
//Removes checked box
for (int i=checkedListBox1.CheckedIndices.Count-1; i>=0; i--)
{
backupLocations.RemoveAt(checkedListBox1.CheckedIn dices[i]);
}
checkedListBox1.Items.Clear();
checkedListBox1.Items.AddRange(
backupLocations.ToArray(typeof(string)) as string[] );

saveState();
}
}
}
//**************************ENDCODE***************** ***************//

Jul 21 '05 #1
0 1425

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

Similar topics

1
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...
1
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...
2
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...
3
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
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...
6
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? ...
0
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...
2
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...
4
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. ...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 2 August 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Sept 2023 starting at 18:00 UK time (6PM UTC+1) and finishing at about 19:15 (7.15PM) The start time is equivalent to 19:00 (7PM) in Central...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
14
DJRhino1175
by: DJRhino1175 | last post by:
When I run this code I get an error, its Run-time error# 424 Object required...This is my first attempt at doing something like this. I test the entire code and it worked until I added this - If...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: Mushico | last post by:
How to calculate date of retirement from date of birth
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

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.