gpt4 book ai didi

c# - 抛出 WebException 的代码在 .NET 3.5 中有效,但在 4.0 中无效

转载 作者:太空宇宙 更新时间:2023-11-03 16:34:53 25 4
gpt4 key购买 nike

我正在编写代码来检查文件是否存在。如果文件存在,它应该抛出 WebException。代码如下;如果我在 .NET 3.5 中运行此代码,它会抛出 WebException,但如果使用 .NET 4.0,则不会抛出异常。我希望抛出异常,以便我可以验证文件是否存在。

bool IsExists = true;
try
{
// string ftpServer = "FTP://111.111.111.111/16FebTo15Mar/Backup/MT_Backup/";
string userName = "gff";
// string filename = "sachin";
string password = "gff@123";
string ftppath= "FTP://111.111.111.111/16FebTo15Mar/Backup/MT_Backup/bh/";
// Ftpurl + new FileInfo(EmpFldr).Name + "/";

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(ftppath);
request.Credentials = new NetworkCredential(userName, password);
request.Method = WebRequestMethods.Ftp.PrintWorkingDirectory;
// request.Method = WebRequestMethods.Ftp.MakeDirectory;
FtpWebResponse response = (FtpWebResponse)request.GetResponse();
}
catch (WebException ex)
{
IsExists = false;
}
return IsExists;

最佳答案

如果你想通过 FTP 检查文件是否存在,那么 check out this answer .

I'd recommend using the following WebRequestMethod instead, this works for me on all servers I tested, even ones which would not return a file size.

WebRequestMethods.Ftp.GetDateTimestamp

关于c# - 抛出 WebException 的代码在 .NET 3.5 中有效,但在 4.0 中无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9426317/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com