473,378 Members | 1,422 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 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 1473

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. ...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.