C#File.exists路径
我制作了一个重命名文件夹中文件的程序,它可以工作,但有一个问题,如果我添加更多文件,然后用该程序重命名它,它会覆盖其他文件,因此我尝试了C#File.exists路径,c#,.net,file,c#-4.0,C#,.net,File,C# 4.0,我制作了一个重命名文件夹中文件的程序,它可以工作,但有一个问题,如果我添加更多文件,然后用该程序重命名它,它会覆盖其他文件,因此我尝试了如果(!File.Exists),但它一直不工作。如果(!File.Exists)部分,任何人都可以帮助?因为这个函数不起作用,并且总是返回true Console.WriteLine("1. Rename all\n2. Rename Custom"); int Choice = int.Parse(Console.ReadLine()); Console.C
如果(!File.Exists)
,但它一直不工作。如果(!File.Exists)部分,任何人都可以帮助?因为这个函数不起作用,并且总是返回true
Console.WriteLine("1. Rename all\n2. Rename Custom");
int Choice = int.Parse(Console.ReadLine());
Console.Clear();
if(Choice==1)
{
Console.WriteLine("Enter the file path:");
string path = Console.ReadLine();
Console.WriteLine("Enter the new file type");
string type = Console.ReadLine();
DirectoryInfo d = new DirectoryInfo(@path);
FileInfo[] infos = d.GetFiles("*.*");
int i = 1;
foreach (FileInfo f in infos)
{
// Do the renaming here
if (!File.Exists(@path+i+"."+type))
File.Move(f.FullName, Path.Combine(f.DirectoryName, "" + i + "." + type));
i++;
}
}
首先,您不需要@path
<没有@
的代码>路径将正常工作。其次,要检查的文件与要移动的目标路径不匹配。请尝试以下方法:
string destination = Path.Combine(f.DirectoryName, string.Format("{0}.{1}", i, type));
if (!File.Exists(destination))
{
File.Move(f.FullName, destination);
i++; // Unclear if you want this to increment every time or just when moving
}
首先,您不需要@path
<没有@
的代码>路径将正常工作。其次,要检查的文件与要移动的目标路径不匹配。请尝试以下方法:
string destination = Path.Combine(f.DirectoryName, string.Format("{0}.{1}", i, type));
if (!File.Exists(destination))
{
File.Move(f.FullName, destination);
i++; // Unclear if you want this to increment every time or just when moving
}