C#如何给文件重命名
答案:2 悬赏:10 手机版
解决时间 2021-02-16 05:50
- 提问者网友:骨子里的高雅
- 2021-02-15 21:34
我生成的文件在temp下,每次把文件copy到back下,在把temp的文件删掉,怎么样才能把back中复制过来的的文件重命名,temp下的文件名格式都为 YYYYMMdd.txtd的,所以如何判断back下是否有相同的文件,然后再重命名。(最少10次)
最佳答案
- 五星知识达人网友:琴狂剑也妄
- 2021-02-15 21:59
我写了个winform的控制台应用程序。你看看。 static void Main(string[] args)
{
string path = @"d:\4.txt"; //源文件
string path2 = @"d:\move1\3.txt";//移动到哪里
if (MoveFile(path, path2))
Console.WriteLine("success");
else
Console.WriteLine("bad"); Console.WriteLine();
}
private static bool MoveFile(string OldPath, string MoveNewPath)
{
Boolean IsSuccess = false;//状态 try
{
if (!File.Exists(OldPath))//判断路径是是否存在
{
Console.WriteLine("不存在,正在创建");
using (FileStream fs = File.Create(OldPath)) { } //创建文件
}
if (!File.Exists(MoveNewPath))//判断路径是是否存在
{
Console.WriteLine("不存在,正在创建");
using (FileStream fs = File.Create(MoveNewPath)) { } //创建文件
}
if (File.Exists(MoveNewPath))
{
Console.WriteLine("原目录有相同的文件,/r是否覆盖该文件,确定:Y 取消输入:N");
string inputVlaue = Console.ReadLine();
switch (inputVlaue.ToUpper())
{
case "Y":
File.Delete(MoveNewPath);
File.Move(OldPath, MoveNewPath);
IsSuccess = true;
Console.WriteLine("成功将{0}move到{1}.", OldPath, MoveNewPath);
break;
case "N":
IsSuccess = false;
break;
default:
Console.WriteLine("输入有误");
break;
}
}
else
{
File.Move(OldPath, MoveNewPath);
IsSuccess = true;
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
return IsSuccess;
}
{
string path = @"d:\4.txt"; //源文件
string path2 = @"d:\move1\3.txt";//移动到哪里
if (MoveFile(path, path2))
Console.WriteLine("success");
else
Console.WriteLine("bad"); Console.WriteLine();
}
private static bool MoveFile(string OldPath, string MoveNewPath)
{
Boolean IsSuccess = false;//状态 try
{
if (!File.Exists(OldPath))//判断路径是是否存在
{
Console.WriteLine("不存在,正在创建");
using (FileStream fs = File.Create(OldPath)) { } //创建文件
}
if (!File.Exists(MoveNewPath))//判断路径是是否存在
{
Console.WriteLine("不存在,正在创建");
using (FileStream fs = File.Create(MoveNewPath)) { } //创建文件
}
if (File.Exists(MoveNewPath))
{
Console.WriteLine("原目录有相同的文件,/r是否覆盖该文件,确定:Y 取消输入:N");
string inputVlaue = Console.ReadLine();
switch (inputVlaue.ToUpper())
{
case "Y":
File.Delete(MoveNewPath);
File.Move(OldPath, MoveNewPath);
IsSuccess = true;
Console.WriteLine("成功将{0}move到{1}.", OldPath, MoveNewPath);
break;
case "N":
IsSuccess = false;
break;
default:
Console.WriteLine("输入有误");
break;
}
}
else
{
File.Move(OldPath, MoveNewPath);
IsSuccess = true;
}
}
catch (Exception e)
{
Console.WriteLine("The process failed: {0}", e.ToString());
}
return IsSuccess;
}
全部回答
- 1楼网友:鸠书
- 2021-02-15 23:28
用file.move
原形:
public static void move (
string sourcefilename,
string destfilename
)
参数
sourcefilename
要移动的文件的名称。
destfilename
文件的新路径。
using system;
using system.io;
class test
{
public static void main()
{
string path = @"c:\temp\mytest.txt";
string path2 = @"c:\temp2\mytest.txt";
try
{
if (!file.exists(path))
{
// this statement ensures that the file is created,
// but the handle is not kept.
using (filestream fs = file.create(path)) {}
}
// ensure that the target does not exist.
if (file.exists(path2))
file.delete(path2);
// move the file.
file.move(path, path2);
console.writeline("{0} was moved to {1}.", path, path2);
// see if the original exists now.
if (file.exists(path))
{
console.writeline("the original file still exists, which is unexpected.");
}
else
{
console.writeline("the original file no longer exists, which is expected.");
}
}
catch (exception e)
{
console.writeline("the process failed: {0}", e.tostring());
}
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯