How to copy and replace folder in desktop with C#/Winforms?
-
Hi guys, I want when i have a radio button checked by pressing a button the desired folder with all files in it to be in desktop and replacing anything. I have this :
public partial class Form1 : Form
{string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
and then :
private void button1_Click(object sender, EventArgs e)
{foreach (string file in Directory.GetFiles(SourceOneToCopyBox.Text)) { File.Copy(file, Path.Combine(OutputDirectory1.Text, Path.GetFileName(file)), true); if (YesRadioButton.Checked == true) { File.Copy(file, Path.Combine(desktop, Path.GetFileName(file)), true); } } }
Thank you in advance.While the code works for every directory everywhere I can't do anything with the directory I want in desktop.
-
Hi guys, I want when i have a radio button checked by pressing a button the desired folder with all files in it to be in desktop and replacing anything. I have this :
public partial class Form1 : Form
{string desktop = Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory);
and then :
private void button1_Click(object sender, EventArgs e)
{foreach (string file in Directory.GetFiles(SourceOneToCopyBox.Text)) { File.Copy(file, Path.Combine(OutputDirectory1.Text, Path.GetFileName(file)), true); if (YesRadioButton.Checked == true) { File.Copy(file, Path.Combine(desktop, Path.GetFileName(file)), true); } } }
Thank you in advance.While the code works for every directory everywhere I can't do anything with the directory I want in desktop.
-
octapuslabs wrote:
I can't do anything with the directory I want in desktop.
I am afraid that is not clear. Please explain exactly what problem you see at this point.
Let's say I have a directory c:\test with some files in it.I want this directory to be copied in desktop with every file in it.If it exists I want to replace it with the new files .I m making a program so I can have backup of my projects easily and not copying pasting with windows clicks all the time . This is the whole code which creates a folder in desktop Downloads\Downloads and also some files are copies to desktop\Downloads and some others to desktop\Downloads\Downloads.
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;
using System.IO;//We need this namespace to be used so we can have the file class
//that gives us the ability to copy,delte files etc.namespace EasyCopyFiles
{
public partial class Form1 : Form
{string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); public Form1() { InitializeComponent(); } private void button1\_Click(object sender, EventArgs e) { foreach (string file in Directory.GetFiles(SourceOneToCopyBox.Text))//SourceOneToCopy //textbox we read from user input exammple c:\\test { //OutputDirectory1 also a text box we read from user example d:\\test File.Copy(file, Path.Combine(OutputDirectory1.Text, Path.GetFileName(file)), true); if (YesRadioButton.Checked == true) { desktop += "\\\\Downloads";//With the \\\\ we create the path we want if (!System.IO.Directory.Exists(desktop)) { DirectoryInfo directory = Directory.CreateDirectory(desktop); } File.Copy(file, Path.Combine(desktop, Path.GetFileName(file)), true); } } } }
}
Thank you again I m so close to it.Thank you a lot.
-
Let's say I have a directory c:\test with some files in it.I want this directory to be copied in desktop with every file in it.If it exists I want to replace it with the new files .I m making a program so I can have backup of my projects easily and not copying pasting with windows clicks all the time . This is the whole code which creates a folder in desktop Downloads\Downloads and also some files are copies to desktop\Downloads and some others to desktop\Downloads\Downloads.
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;
using System.IO;//We need this namespace to be used so we can have the file class
//that gives us the ability to copy,delte files etc.namespace EasyCopyFiles
{
public partial class Form1 : Form
{string desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); public Form1() { InitializeComponent(); } private void button1\_Click(object sender, EventArgs e) { foreach (string file in Directory.GetFiles(SourceOneToCopyBox.Text))//SourceOneToCopy //textbox we read from user input exammple c:\\test { //OutputDirectory1 also a text box we read from user example d:\\test File.Copy(file, Path.Combine(OutputDirectory1.Text, Path.GetFileName(file)), true); if (YesRadioButton.Checked == true) { desktop += "\\\\Downloads";//With the \\\\ we create the path we want if (!System.IO.Directory.Exists(desktop)) { DirectoryInfo directory = Directory.CreateDirectory(desktop); } File.Copy(file, Path.Combine(desktop, Path.GetFileName(file)), true); } } } }
}
Thank you again I m so close to it.Thank you a lot.
-
The problem is that either I can create a new empty folder or if the folder already exists it replaces the files .If the folder doesn't exist I get an error a bug or something which crashes.
-
The problem is that either I can create a new empty folder or if the folder already exists it replaces the files .If the folder doesn't exist I get an error a bug or something which crashes.
octapuslabs wrote:
an error a bug or something which crashes.
Well, since we cannot see your screen, we cannot guess which, or what any of them might say. Please provide all the information relating to your problem, including the full details of the error, and the point in your code where it occurs.
-
octapuslabs wrote:
an error a bug or something which crashes.
Well, since we cannot see your screen, we cannot guess which, or what any of them might say. Please provide all the information relating to your problem, including the full details of the error, and the point in your code where it occurs.
System.IO.DirectoryNotFoundException
{"Could not find a part of the path 'd:\\test\\130123094607-kevin-durant-dunk-lamar-odom-012313.1200x672.jpg'."}
This happens when we have for example to copy c:\test to d:\test and the d:\ doesn't have the folder.If I create the folder manually the error disappears but the thing is I want the folder to be created if it doesn't exist and if it does exist to overwrite every file in it. Thank you again for your time.
-
System.IO.DirectoryNotFoundException
{"Could not find a part of the path 'd:\\test\\130123094607-kevin-durant-dunk-lamar-odom-012313.1200x672.jpg'."}
This happens when we have for example to copy c:\test to d:\test and the d:\ doesn't have the folder.If I create the folder manually the error disappears but the thing is I want the folder to be created if it doesn't exist and if it does exist to overwrite every file in it. Thank you again for your time.
-
Then you need first to test if the directory exists, and if not then create it. Similarly when creating files, if they exist in the folder you need to ensure you use a create option that will force it to a new file.
Yup I thought of that creating first the directory and then copying files but the thing I can t do both.I m having some problem probably in code which I hope I ll find it slowly.The problem is that I can t copy the files when creating the new folder only if it s already there.Thank you again sir.
-
Yup I thought of that creating first the directory and then copying files but the thing I can t do both.I m having some problem probably in code which I hope I ll find it slowly.The problem is that I can t copy the files when creating the new folder only if it s already there.Thank you again sir.
It's a two step process. Check to see if the directory exists that you want to copy into and then do the copy. That's what you have to do if you want to stay in the .NET world (of course, you could always spin up a Process and run an xcopy to do the requisite copy).
-
It's a two step process. Check to see if the directory exists that you want to copy into and then do the copy. That's what you have to do if you want to stay in the .NET world (of course, you could always spin up a Process and run an xcopy to do the requisite copy).
Yup I m new and I know net is great.I ll find it slowly.
-
Yup I m new and I know net is great.I ll find it slowly.
Okay, you can take advantage of the fact that Directory.CreateDirectory only creates a directory if it doesn't exist so you could do the following:
private void CopyFiles(string sourceDirectory, string targetDirectory)
{
Directory.CreateDirectory(targetDirectory);
string[] files = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
string targetFile = Path.Combine(targetDirectory, file.Replace(sourceDirectory, string.Empty));
File.Copy(file, targetFile);
}
}I've just typed this into the editor, so you might need to fix a syntax mistake or two, but this should roughly do what you're trying to accomplish.
-
Okay, you can take advantage of the fact that Directory.CreateDirectory only creates a directory if it doesn't exist so you could do the following:
private void CopyFiles(string sourceDirectory, string targetDirectory)
{
Directory.CreateDirectory(targetDirectory);
string[] files = Directory.GetFiles(sourceDirectory, "*.*", SearchOption.AllDirectories);
foreach (string file in files)
{
string targetFile = Path.Combine(targetDirectory, file.Replace(sourceDirectory, string.Empty));
File.Copy(file, targetFile);
}
}I've just typed this into the editor, so you might need to fix a syntax mistake or two, but this should roughly do what you're trying to accomplish.
Thank you sir a lot .I ll try in next days and I ll tell you result.