[英]C# File.exists path
I have made a program that renames files in a folder, and it works, but there is one problem, if i add more files and then rename it with the program it overwrites the other files, so i have tried if(!File.Exists)
but it keeps not working. 我已经制作了一个程序来重命名文件夹中的文件,并且它可以工作,但是有一个问题,如果我添加更多文件,然后使用该程序重命名它会覆盖其他文件,所以我尝试了if(!File.Exists)
但仍然无法正常工作。 anyone can help with the if(!File.Exists)
part? 任何人都可以帮助if(!File.Exists)
部分吗? cus this one doesn't work and always returns true cus this this不起作用,总是返回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++;
}
}
First of all, you don't need @path
. 首先,您不需要@path
。 path
without the @
will work just fine. 没有@
path
将正常工作。 Second, the file you're checking the existence of doesn't match the destination path for moving. 其次,您要检查的文件是否与移动的目标路径不匹配。 Try this instead: 尝试以下方法:
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
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.