开发者

The following code to check if a file exists on a server does not work

I found the following code to check if a file exists on a server, but is not working for me. It tells me that "test1.tx" does not exist even though the file exists and its size is 498 bytes. If I try with Ftp.ListDirectory it tells me that the file does not exist. If I try with Ftp.GetFileSize it does not provide any results and the debugger's immediate gives me the following message: A first chance exception of type 'System.Net.WebException' occurred in System.dll.

Using "request.UseBinary = true" does not make any difference.

I have posted this same question at this link: http://social.msdn.microsoft.com/Forums/en-US/ncl/thread/89e05cf3-189f-48b7-ba28-f93b1a9d44ae

Could someone help me how to fix it?

   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");
                }
            }
        }
    }

Thanks very much, xplorer2k


Escape characters in C# are defined with a "\" character, so I think there shouldn't be a problem with the single-slash before the filename, however, I would not say the same from the double-slash following the ftpServerIP variable in the string. To make sure, type it as you would normally using the the at sign.

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


When you connect to an FTP server you might specify the Uri as "ftp//ftp.domain.com/somedirectory" but this translates to: "ftp://ftp.domain.com/homedirectoryforftp/somedirectory". To be able to define the full root directory use "ftp://ftp.domain.com//somedirectory" which translates to //somedirectory on the machine.


which line does the error occur on when you try ftp.getfilesize?


Here is what you do if you want to delete a file atleast. Would imagine you could just change it from DeleteFile to GetFileSize.

Are you sure your network credentials are set up correctly, meaning could there be anything wrong with the strings that are passed along as your username and password ?

if (serverUri.Scheme != Uri.UriSchemeFtp)
{
    return false;
}
// Get the object used to communicate with the server.
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(serverUri);
request.Method = WebRequestMethods.Ftp.DeleteFile;

FtpWebResponse response = (FtpWebResponse) request.GetResponse();
Console.WriteLine("Delete status: {0}",response.StatusDescription);  
response.Close();
return true;


You have an extra forward slash in the first line of your try:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create("ftp://" + ftpServerIP + "//tmp/test1.txt");

should be:

FtpWebRequest request = (FtpWebRequest)WebRequest.Create(@"ftp://" + ftpServerIP + "/tmp/test1.txt");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜