Click here to Skip to main content
15,891,033 members
Please Sign up or sign in to vote.
2.00/5 (1 vote)
See more:
Hi, I am finding difficult on copying a file from folder to folder, actually in a Folder A there are audio files of mp3, wav and dss, i want to copy the file by giving the filename but without extension. Below is the code, if i try i am getting error as illegal path.

Kindly help

C#
string filenumber = textBox1.Text ;

string soureFile = @"C:\Folder A\";
string targetFile = @"D:\Audios\";

string sourceFile = System.IO.Path.Combine(soureFile, filenumber);
string destFile = System.IO.Path.Combine(targetFile, filenumber);

string chifex = sourceFile + filenumber;
if (File.Exists(chifex))
{
    System.IO.File.Copy(sourceFile, destFile, true);
}
else
{
    System.IO.File.Copy(sourceFile, destFile);
}
Posted
Updated 23-Nov-11 19:03pm
Comments
karylle 24-Nov-11 1:23am    
what happens if "C:\Folder A\" contains song1.mp3 and song1.wav? which file will be copied? why is the extension ignored?

It is not possible to do what you are trying to do. To access a file, you are required to give the full file name including the extension. Sorry!
 
Share this answer
 
C#
string[] files = Directory.GetFiles(soureFile, filenumber + ".*");
foreach (string file in files)
    System.IO.File.Copy(file, System.IO.Path.Combine(targetFile,System.IO.Path.GetFileName(file)));
 
Share this answer
 
Comments
jaipe 24-Nov-11 1:51am    
Thank you very much, worked well :)
Prerak Patel 24-Nov-11 1:52am    
You are welcome.
It is not possible directly but you can do by customizing your code.
following code gives you list of files regardless of extension

C#
string[] filePaths = Directory.GetFiles(@"C:\MyDir\", "1.*",SearchOption.AllDirectories);


it will return files having name 1 with all extensions.
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900