我发现很多帖子都在检查ftp服务器上的文件是否存在。提供的代码都没有帮助。我试图自动上传小文件到ftp服务器但我不想覆盖文件,如果它已经存在。如果文件已经存在,则以下代码有效,但如果不存在,则会返回错误,并且不会在if语句中运行代码。
System.Console.WriteLine("check if hash file is on ftp server " + hashfile);
string fileSize = ftpClient.getFileSize(hashfile);
int fSize =-1;
bool res = (Int32.TryParse(fileSize, out fSize));
if (res)
{
System.Console.WriteLine("Hash file is not on ftp server " + hashfile);
ftpClient.upload(hashfile, @hashfile);
ftpClient.upload(charfile, @charfile);
File.Delete(hashfile);
File.Delete(charfile);
}
else
{
System.Console.WriteLine("Hash file is on ftp server " + hashfile);
string newHashFile = "ntlmhash" + str_cint + "_" + str_pint + "_" + e + ".txt";
while (File.Exists(newHashFile))
{
e++;
newHashFile = "ntlmhash" + str_cint + "_" + str_pint + "_" + e + ".txt";
string FileSize = ftpClient.getFileSize(newHashFile);
int FSize =-1;
if (Int32.TryParse(FileSize, out FSize))
{
ftpClient.upload(newHashFile, @newHashFile);
ftpClient.upload(charfile, @charfile);
File.Delete(newHashFile);
File.Delete(charfile);
}
else
{
e++;
}
}
System.IO.File.Move(hashfile, newHashFile);
ftpClient.upload(newHashFile, @newHashFile);
ftpClient.upload(charfile, @charfile);
File.Delete(newHashFile);
File.Delete(charfile);
}