Solved

Save checkedListBox file list to USB drive

Posted on 2009-05-13
2
630 Views
Last Modified: 2012-06-21
I am trying to save a list of files in my checkedListBox to a drive.

I have searched and attempted to use a number of examples on the web.  Including those listed in the Code section and many others.

What would be the easiest operation to run?
File.Copy(sourceFileName, strDrive, overwrite);
 

            // create a file stream, where "c:\\testing.txt" is the file path

            System.IO.FileStream fs = new System.IO.FileStream("saveFileDialog1.FileName", System.IO.FileMode.OpenOrCreate, System.IO.FileAccess.Write, System.IO.FileShare.ReadWrite);
 

            // create a stream writer

            System.IO.StreamWriter sw = new System.IO.StreamWriter(fs, System.Text.Encoding.ASCII);
 

            // write to file (buffer), where textbox1 is your text box

            sw.Write(txtDestination.Text);
 

            // flush buffer (so the text really goes into the file)

            sw.Flush();
 

            // close stream writer and file

            sw.Close(); fs.Close();

        }
 
 
 
 
 

    // Store in a string array

            string[] drives = Environment.GetLogicalDrives();

            // Loop into the string array

            foreach (string strDrive in drives)

            {

                // Add items (drives) to the list

                checkedListBox2.Items.Add(strDrive.ToString());

            }
 

            // Insert code to save a file.

            

            foreach (int indexChecked in checkedListBox1.CheckedIndices)

            {

                // The indexChecked variable contains the index of the item.

                MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" +

                                checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");

            }

Open in new window

0
Comment
Question by:pk4000
2 Comments
 
LVL 7

Accepted Solution

by:
deadlyDev earned 125 total points
ID: 24516394
Just by modifying your code slightly...
// Insert code to save a file.

StringBuilder sb = new StringBuilder();            

            foreach (int indexChecked in checkedListBox1.CheckedIndices)

            {

                // The indexChecked variable contains the index of the item.

                //MessageBox.Show("Index#: " + indexChecked.ToString() + ", is checked. Checked stateis:" 

                //+ checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
 

                sb.AppendLine("Index#: " + indexChecked.ToString() + ", is checked. Checked state is:" + 

                checkedListBox1.GetItemCheckState(indexChecked).ToString() + ".");
 

            }
 

System.IO.File.WriteAllText("File location on drive...", sb.ToString());

Open in new window

0
 
LVL 1

Author Closing Comment

by:pk4000
ID: 31581069
Thanks, had to make some changes because of multiple Panels but it worked great!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Extention Methods in C# 3.0 by Ivo Stoykov C# 3.0 offers extension methods. They allow extending existing classes without changing the class's source code or relying on inheritance. These are static methods invoked as instance method. This…
It was really hard time for me to get the understanding of Delegates in C#. I went through many websites and articles but I found them very clumsy. After going through those sites, I noted down the points in a easy way so here I am sharing that unde…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.
This video discusses moving either the default database or any database to a new volume.

744 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now