By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,218 Members | 1,236 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,218 IT Pros & Developers. It's quick & easy.

Disappearing data from StreamWriter

P: n/a
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
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.