主题:如何在复制文件的同时修改文件名
我要写一段代码实现这样的功能:对于一个给定的C#文件,将该文件复制的同时以新的文件名进行保存,例如源文件名是abc.cs,复制保存时将文件名改为abc1.cs.我的代码如下:
namespace filecopy2
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class CopyFile
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Console.WriteLine("please input the address:");
string strFileName=Console.ReadLine();
if(File.Exists(strFileName))
{
string ls=strFileName;//文件名的提取有错误
string sre="";
char[] ch=ls.ToCharArray();
for(int i=0;i<ls.Length;i++)
{
//string sre="";
do
{
sre=sre+ch[i].ToString();
}while(ch[i]!='.');
}
string strfilename=sre;
File.Copy(strFileName,strfilename+"2.cs",true);
}
else Console.WriteLine("文件不存在!");
}
}
}
调试运行不出结果,请高手帮忙给看一下.非常感谢!
namespace filecopy2
{
/// <summary>
/// Class1 的摘要说明。
/// </summary>
class CopyFile
{
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main(string[] args)
{
//
// TODO: 在此处添加代码以启动应用程序
//
Console.WriteLine("please input the address:");
string strFileName=Console.ReadLine();
if(File.Exists(strFileName))
{
string ls=strFileName;//文件名的提取有错误
string sre="";
char[] ch=ls.ToCharArray();
for(int i=0;i<ls.Length;i++)
{
//string sre="";
do
{
sre=sre+ch[i].ToString();
}while(ch[i]!='.');
}
string strfilename=sre;
File.Copy(strFileName,strfilename+"2.cs",true);
}
else Console.WriteLine("文件不存在!");
}
}
}
调试运行不出结果,请高手帮忙给看一下.非常感谢!