Save checkedListBox file list to USB drive

Posted on 2009-05-13
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
            // flush buffer (so the text really goes into the file)
            // 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
            // 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

Question by:pk4000
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions

Accepted Solution

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


Author Closing Comment

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

Featured Post


Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Visualize your data even better in Access queries. Given a date and a value, this lesson shows how to compare that value with the previous value, calculate the difference, and display a circle if the value is the same, an up triangle if it increased…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

626 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