仅通过提供不带扩展名的文件名来复制文件 [英] Copying file only by giving the filename without extension

查看:116
本文介绍了仅通过提供不带扩展名的文件名来复制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我发现很难从一个文件夹到另一个文件夹复制文件,实际上在文件夹A中有mp3,wav和dss音频文件,我想通过提供文件名但不带扩展名来复制文件.下面是代码,如果我尝试将错误作为非法路径.

请帮助

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

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);
}

推荐答案

无法做您想做的事情.要访问文件,您需要提供完整的文件名,包括扩展名.抱歉!
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!


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)));


它是无法直接实现,但可以通过自定义代码来实现.
以下代码为您提供文件列表,而不管扩展名

It is not possible directly but you can do by customizing your code.
following code gives you list of files regardless of extension

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



它将返回名称为1且具有所有扩展名的文件.



it will return files having name 1 with all extensions.


这篇关于仅通过提供不带扩展名的文件名来复制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆