如何从C#.net文件夹中删除上传的文件 [英] how to delete the uploaded file from the folder in C#.net

查看:138
本文介绍了如何从C#.net文件夹中删除上传的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在数据库中上传了一个文件,该文件也包含在asp.net网站页面的文件夹中。

现在我要从数据库和文件夹中删除该文件也。我已经从数据库中成功删除了

文件,但文件没有从网站的文件夹中删除。

请告诉我正确的答案。

整个细节如下: -



首先我上传了一个文件(无论是图片,文件等)现在你可以

了解该文件也将转到该文件夹​​。这意味着该文件将转到数据库(sql server)并转到该网站的文件夹。



这是我的c#代码,用于从数据库和文件夹中删除文件。文件夹名称是Uploads,您可以在代码中看到,存储过程是PR_Brand_DeleteByPK;所以现在您可以理解该文件正在从数据库中删除。但不能从上传文件夹中删除。



代码如下: -

I had uploaded a file in the database and that file is also contains by a folder of the website page in asp.net.
Now i want to delete the file from the database and the folder also. I have successfully deleted the
file from the database but file is not deleting from the folder of the website.
so tell me the proper answer.
The whole detail is given below:-

Firstly I had uploaded a file(whether it is image,word file etc.)Now u can
understand that file will also go to the folder .It mean to say that file will go the database(sql server) and also go to the folder of the website.

Here is my c# code for delete a file from the database and the folder also.The folder name is "Uploads" which you can see in the code and the store procedure is "PR_Brand_DeleteByPK"; so now you can understand that file is deleting from the database. but not deleting from the folder "Uploads".

Code is given below:-

protected void gvBrand_RowCommand(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName == "Delete")
        {
            SqlConnection objConn = new SqlConnection(ConfigurationManager.ConnectionStrings["BuildMyPCConnectionString"].ToString());
            objConn.Open();

            SqlCommand objCmd = objConn.CreateCommand();
            objCmd.CommandType = CommandType.StoredProcedure;

            objCmd.CommandText = "PR_Brand_DeleteByPK";
            objCmd.Parameters.AddWithValue("@BrandID", Convert.ToInt32(e.CommandArgument.ToString()));

            
            FileInfo path = new FileInfo(Server.MapPath("~/Upload/filename" ));
            path.Delete(); // i try this//

            
            objCmd.ExecuteNonQuery();
            gvBrand.DataBind();
        }
    }

推荐答案

这有两个原因:

首先,上传与上传不同 - 您的文字说文件夹是上传,代码显示上传。

其次,因为它不太可能您要删除的文件称为文件名 - 我怀疑您需要在调用Server.MapPath方法之前将文件名合并到字符串中。



一次你修复它们,再试一次 - FileInfo.Delete应该可以工作。如果没有,那么请检查您尝试根据从Server.MapPath返回的值手动删除的文件名。
It''s not working for two reasons:
Firstly, "Uploads" is not the same as "Upload" - your text says the folder is "Uploads" and the code shows "Upload" instead.
Secondly, because it is unlikely that the file you want to remove is called "filename" - I suspect that you need to incorporate the file name into the string before you call the Server.MapPath method.

Once you fix them, try again - FileInfo.Delete should work. If it doesn''t, then check the file name you are trying to remove manually against the value you get back from Server.MapPath.


hi !!!我写了这个代码从文件夹中删除图片

看看这应该对你有帮助



hi!!! i m written this code for delete pic from folder
take a look of this it should helps you

protected void DelBtn_Click(object sender, EventArgs e)
       {

           if (ListBox1.SelectedIndex== 0)
           {
               Itemessage.Visible = true;
               Itemessage.Text = "plz select another Image from box it is main pic ..";
               return;

           }


       if( ListBox1.SelectedItem==null)
       {
           Itemessage.Visible = true;
           Itemessage.Text = "plz select Image from box first..";
           return;

       }
           if (ListBox1.SelectedIndex > -1)
           {
               string _value = ListBox1.SelectedItem.Value; //Gets the value of items in list.
               string _text = ListBox1.SelectedItem.Text;  // Gets the Text of items in the list.
               ListItem item = new ListItem(); //create a list item
               item.Text = _text;               //Assign the values to list item
               item.Value = _value;
               string completePath = Server.MapPath("~/ItemsPics/" + item.Text);

               if (System.IO.File.Exists(completePath))
               {

                   System.IO.File.Delete(completePath);
                }
               ListBox1.Items.Remove(item); //Remove from the selected list
               searchimg();
           }

      }


这篇关于如何从C#.net文件夹中删除上传的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆