Solved

Save checkedListBox file list to USB drive

Posted on 2009-05-13
2
632 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

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

Question has a verified solution.

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

Suggested Solutions

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

831 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