I'm trying to create a copy button for files with this specific function:
When I write in the textbox for example "KL5050" and then press the copy button, I want to copy the file KL5050 from a specific folder and paste it to another specific folder without asking or opening any dialog or window, just click, copy and paste.
It doesn't matter the file format, it could be TXT, doc, PDF, JPEG, etc., as long as it matches the text from the textbox to any existing file in the folder it will be copied to another folder.
Also, if the file doesn't exist from the copy folder, get a warning "file not found".
The code that I have so far has two textboxes, the first is called serieBox, that one only requires input data (if is empty opens a window) before proceeding to the next box, then the next box is named recetaBox, which is the one where I type the name of file and then clicking the button, the program will look for the file and copy it to the different folder. So far I don't know where to put the second box, recetaBox. Is not in the code yet.
private void cargarButton_Click(object sender, EventArgs e)
{
if (serieBox.Text == string.Empty)
{
SerieWin openForm = new SerieWin();
openForm.ShowDialog();
}
else
{
try
{
string dircopyFrom = @"C:\From\";
string dircopyTo = @"C:\To\";
string[] files = Directory.GetFiles(dircopyFrom);
foreach (string file1 in files)
{
string filename1 = Path.GetFileName(file1);
File.Copy(file1, dircopyTo "\\", true);
}
}
catch (Exception ex)
{
}
CodePudding user response:
The only thing you haven't been totally clear on is whether KL5050 is the start, end, whole or part of the FileName but it's easy to fix
string[] files = Directory.GetFiles(dircopyFrom);
foreach (string file in files)
{
var fn = Path.GetFileNameWithoutExtension(file);
if(!fn.Contains(recetaTextBox.Text, StringComparison.OrdinalIgnoreCase))
continue;
fn = Path.GetFileName(file);
File.Copy(file, Path.Combine(dircopyTo, fn), true);
}
} catch(Exception ex){
MessageBox.Show(ex.Message);
}
Take away points:
do some check like Contains, StartsWith, EndsWith etc
use Path.Combine to build paths; .net runs on more than just windows and different systems have different directory separator chars
File.Copy takes a destination path that must contain a FileName too, not just a directory. The destination file can be renamed during copy by tweaking the name
don't ever put an empty catch block. At the very least always do a
MessageBox.Show(ex.Message);
especially working with files. Having a program that does nothing when a button is clicked is very frustrating. If at least you get a "file is in use by another program" when trying to copy, then you can do something about it.. but if you swallow that exception and throw it away rather than surfacing it then you'll have a really tough time tracking down any problemsif you want a slight efficiency boost you can use the contents of
recetaTextBox.Text
to form a filter to GetFiles, e.gGetFiles(dircopyFrom, recetaTextBox.Text "*.*")
- that is conceptually equivalent to "StartsWith". You can read more about getfiles that takes a search pattern in the fine manual - pattern matching is very basic though, if you want any advanced stuff like Regex, or even just case sensitivity, you'll need to do it in your own codethe directory you're copying to has to exist. I didn't put anything in the code to ensure this but note that it's safe to call
Directory.CreateDirectory(dirCopyTo)
even if it already exists, so if there's a chance the dir won't exist, you can always call CreateDirectory before you call copy to make sure it does