Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Newbie move file code questions 1

Status
Not open for further replies.

theniteowl

Programmer
May 24, 2005
1,975
US
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.
 
Hi Trent,

1) Simple: set the CheckedListBox property "CheckOnClick" to true. ;-)

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:
[b]var ToBeDeleted = new List<object>();[/b]

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);
              [b]ToBeDeleted.Add(itemChecked);[/b]
          }
[b]foreach(object item in ToBeDeleted)
{
    checkListBox1.Items.Remove(item);
}[/b]

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.
 
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.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top