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************ *************** *****//