gpt4 book ai didi

c# - 以下用于检查服务器上是否存在文件的代码不起作用

转载 作者:太空宇宙 更新时间:2023-11-03 11:49:38 26 4
gpt4 key购买 nike

我找到了以下代码来检查服务器上是否存在文件,但对我不起作用。它告诉我“test1.tx”不存在,即使该文件存在并且其大小为 498 字节。如果我尝试使用 Ftp.ListDirectory,它会告诉我该文件不存在。如果我尝试使用 Ftp.GetFileSize,它不会提供任何结果,并且调试器会立即给我以下消息:System.dll 中出现类型为“System.Net.WebException”的第一次机会异常。

使用“request.UseBinary = true”没有任何区别。

我在这个链接上发布了同样的问题:http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/89e05cf3-189f-48b7-ba28-f93b1a9d44ae

谁能帮我解决一下?

   private void button1_Click(object sender, EventArgs e)
{
string ftpServerIP = txtIPaddress.Text.Trim();
string ftpUserID = txtUsername.Text.Trim();
string ftpPassword = txtPassword.Text.Trim();

try
{
FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpServerIP + "//tmp/test1.txt");
request.Method = WebRequestMethods.Ftp.ListDirectory;
//request.Method = WebRequestMethods.Ftp.GetFileSize;
request.Credentials = new NetworkCredential(ftpUserID, ftpPassword);
//request.UseBinary = true;

using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
{
// Okay.
textBox1.AppendText(Environment.NewLine);
textBox1.AppendText("File exist");
}
}
catch (WebException ex)
{
if (ex.Response != null)
{
FtpWebResponse response = (FtpWebResponse)ex.Response;
if (response.StatusCode == FtpStatusCode.ActionNotTakenFileUnavailable)
{
// Directory not found.
textBox1.AppendText(Environment.NewLine);
textBox1.AppendText("File does not exist");
}
}
}
}

非常感谢,xplorer2k

最佳答案

C# 中的转义字符是用“\”字符定义的,所以我认为文件名前的单斜杠应该没有问题,但是,我不会说跟在文件名后面的双斜杠一样字符串中的 ftpServerIP 变量。为确定起见,请像通常使用 at 符号一样键入它。

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(string.Format(@"ftp://{0}/tmp/test1.txt", ftpServerIP));

关于c# - 以下用于检查服务器上是否存在文件的代码不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2427483/

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