Reputation: 588
I have written a function of copy to copy files from one dir to another but keep getting exception that "The given path's format is not supported" . Here is my function code:
private void Copy(string letter)
{
string sourceDir = (txtPath.ToString());
string targetDir = letter;
foreach (var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);
}
Upvotes: 0
Views: 82
Reputation: 216353
To refer to the content of a TextBox you use the TextBox.Text
property
private void Copy(string letter)
{
string sourceDir = txtPath.Text.Trim();
string targetDir = letter;
// Check if source and target exists....
if(Directory.Exists(sourceDir) && Directory.Exists(targetDir))
{
foreach (var file in Directory.GetFiles(sourceDir))
File.Copy(file, Path.Combine(targetDir, Path.GetFileName(file)), true);
}
else
{
MessageBox.Show("Source=" + sourceDir + " or Target: " + targetDir + " doesn't exist"):
}
}
Calling the ToString()
method directly on the instance of the TextBox returns the name of the class followed by the text property (Something like "System.Windows.Forms.TextBox, Text:content of the textbox") and obviously this is not a valid path
It is not clear from your comments what is the content of the variable letter. So you should also be sure that the variable targetDir
points to an actual valid path.
(A single drive letter C D or E are not valid paths)
Upvotes: 2