简体   繁体   English

在ftp服务器上删除文件?

[英]Deleting File On Ftp Server?

i am trying to delete a file from my FTP website but i cannot seem to get the code to work could anyone help?, i get no error's it just doesn't do anything. 我正在尝试从FTP网站删除文件,但是我似乎无法使代码正常工作,有人可以帮忙吗?,我没有错误,因为它什么也没做。

private FtpWebRequest deleteRequest(string uri, string method)
{
    var r = (FtpWebRequest)WebRequest.Create(uri);
    string Delete = WebRequestMethods.Ftp.DeleteFile;

    r.Credentials = new NetworkCredential(TxtUsername.Text, TxtPassword.Text);
    r.Method = Delete = listView1.SelectedItems.ToString();
    return r;
}

The rest of the code. 其余代码。

    private void openToolStripMenuItem_Click(object sender, EventArgs e)
    {
        textBox1.Text = TxtServer.Text + listView1.SelectedItems.ToString();
        deleteRequest(textBox1.Text, WebRequestMethods.Ftp.DeleteFile);
    }

You have to call r.GetResponse() . 您必须调用r.GetResponse() You really don't need to return your request from your method. 您确实不需要从方法中返回请求。

You are overwriting the "DELETE" method with whatever is selected in your ListView. 您正在用ListView中选择的任何内容覆盖“ DELETE”方法。


The following line: 下一行:

r.Method = Delete = listView1.SelectedItems.ToString();

is the same as these two: 与这两个相同:

Delete = listView1.SelectedItems.ToString();
r.Method = Delete;

So that Delete first gets the value of whatever is in your SelectedItems , and then you use that value as the Method . 这样Delete首先获取SelectedItems中的值,然后将其用作Method That's probably not what you had in mind. 那可能不是您所想的。

This is the code i ended up using thank you all for the help really appreciate it. 这是我最终使用的代码,谢谢大家的帮助,我们非常感激它。

private void deleteToolStripMenuItem_Click(object sender, EventArgs e)
    {
        ResultLabel.Text = "Deleting: " + listView1.SelectedItems[0].Text;
        ResultLabel.Show();
        this.Refresh();
        textBox1.Text = TxtServer.Text + listView1.SelectedItems[0].Text;
        FtpWebRequest request = (FtpWebRequest)WebRequest.Create(textBox1.Text);
        request.Method = WebRequestMethods.Ftp.DeleteFile;

        request.Credentials = new NetworkCredential(TxtUsername.Text, TxtPassword.Text);
        FtpWebResponse response = (FtpWebResponse)request.GetResponse();
        ResultLabel.Text = "Deleted: " + listView1.SelectedItems[0].Text;
        response.Close();
    }

I just fully rewrote the code and tried again and it worked. 我只是完全重写了代码,然后再试一次,它奏效了。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM