INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Jobs

Newbie move file code questions

Newbie move file code questions

(OP)
Hi Everyone,
I am a newbie with Visual Studio 12 and C# but not to programming.
I am writing a simple script to display files in a specific location and allowing the user to select which files to move.
I have it setup with a checkedListBox control populated automatically with all files in the source location.
When they have selected the files to move they click on the Move button and the files get moved to a specific destination folder not selectable by the user.

The code is very basic and works but I have a couple of questions.

1. When running the code it is necessary to click twice on an item in the box before it applies a check. Is there any way around this so that a single click works?

2. I am currently clearing the entire control and then repopulating it which seems wasteful to me but I was unable to clear each selected element as the file was moved because of the foreach loop being used and it throws an exception deleting an item where there is an incrementer. Is there a cleaner way to remove each item from the list as the associated file is moved? I know I could create an array of the selected files and then delete those elements in a separate loop but thought someone might be able to suggest an easier method.

I need to put in some safety checks to ensure the paths are good, files exist, files were selected, etc, but I will get to that once I have this all working reasonably well.

Here is my code

CODE

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace WindowsFormsApplication2
{

    public partial class Form1 : Form
    {
        const string sourcePath = @"C:\Test\";
        const string destinationPath = @"C:\Test\Temp\";
        public Form1()
        {
            InitializeComponent();
            checkedListBox1.Items.AddRange(System.IO.Directory.GetFiles(@"C:\Test", "*.*"));
        }

        private void checkedListBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
        }

        private void checkedListBox1_Click(object sender, EventArgs e)
        {
        }

        private void btnSendFiles_Click(object sender, System.EventArgs e)
        {
          foreach(object itemChecked in checkedListBox1.CheckedItems) {
              string sourceFile = sourcePath + System.IO.Path.GetFileName(itemChecked.ToString());
              string destinationFile = destinationPath + System.IO.Path.GetFileName(itemChecked.ToString());
              System.IO.File.Move(sourceFile, destinationFile);
          }
          checkedListBox1.Items.Clear();
          checkedListBox1.Items.AddRange(System.IO.Directory.GetFiles(sourcePath, "*.*"));
        }

    }

} 

Thanks!
Trent

At my age I still learn something new every day, but I forget two others.

RE: Newbie move file code questions

Hi Trent,

1) Simple: set the CheckedListBox property "CheckOnClick" to true. winky smile

2) I see nothing wrong with clearing and repopulating. But if you want an alternative that will remove the checked items, this should work:

CODE

var ToBeDeleted = new List<object>();

foreach(object itemChecked in checkedListBox1.CheckedItems) {
              string sourceFile = sourcePath + System.IO.Path.GetFileName(itemChecked.ToString());
              string destinationFile = destinationPath + System.IO.Path.GetFileName(itemChecked.ToString());
              System.IO.File.Move(sourceFile, destinationFile);
              ToBeDeleted.Add(itemChecked);
          }
foreach(object item in ToBeDeleted)
{
    checkListBox1.Items.Remove(item);
} 

3) If you want to be extra sure about properly concatenating your paths (trailing backslash or not?), don't use + operator, use

CODE

System.Io.Path.Combine(sourcePath, System.IO.Path.GetFileName(itemChecked.ToString())); 
instead.

Cheers,
MakeItSo

"Knowledge is power. Information is liberating. Education is the premise of progress, in every society, in every family." (Kofi Annan)
Oppose SOPA, PIPA, ACTA; measures to curb freedom of information under whatever name whatsoever.

RE: Newbie move file code questions

(OP)
Ah, did not realize there was a property for CheckOnClick. I am new to Visual Studio and C# so it will be a while before I become familiar with it all. More than anything it is the environment I am having trouble with, figuring out what goes where and how to find the tools in the GUI but I will get there.

I know clearing and rebuilding is sufficient but if I were writing a more complex and resource heavy application it always pays to have a mind towards efficiency and I thought I would try to learn the best methods first wherever I can.

Thank you for the tip on System.Io.Path.Combine, I have not seen that before. I was unhappy with the way I currently have the paths setup and the trailing backslash especially but I had just taken it from being hardcoded in multiple locations to using a variable and I disliked the idea of having two variables so that one would have the trailing backslash and one would not. I had planned to revisit that code later on when I begin adding error checking. This is my first attempt at anything in C# and I am muddling through but not too badly I think. Mostly I need to learn the Where, When and Why rules for C# and Studio to become a lot more productive.

This app is simple in nature, it allows someone to review zip files in one folder location and select which ones will send to a vendor. The files get moved to an FTP folder where they are automatically pushed via SFTP by another process.
I have setup a listbox on the right side of the screen to show the FTP folder and have it updating whenever files are moved, but I would like to have that listbox update itself if files in the folder change. For instance, when the files get FTPd to the vendor and disappear from the folder I would like that listbox to update.
I have been playing around with the MS example for setting up listeners for file changes but have not found a way to integrate it into my code yet so that the listeners are running while my regular forms application is still active.
This may also be a bit much for this simple a process and it might be better to just use a timer to update the listbox. Is that something easily done?

Thanks for the help, much appreciated!

At my age I still learn something new every day, but I forget two others.

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members!

Resources

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close