theniteowl
Programmer
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
Thanks!
Trent
At my age I still learn something new every day, but I forget two others.
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.