Click here to Skip to main content
15,885,546 members

how to delete the uploaded file from the folder in asp.net

PATEL RAJEEV asked:

Open original thread
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.

Now If i want to delete that file then it is deleting from the databse(sql server)
but not to deleting from the folder of the website(asp.net).Folder name like (image,upload etc.)

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 databse table name is "emp" so now you can understand that file is deleting from the database table "emp" but not deleting from the folder "Uploads".

Code is given below:-

C#
protected void btndelete_Click(object sender, EventArgs e)
    {
 string connectionString = WebConfigurationManager.ConnectionStrings["cnn"].ConnectionString;
        
          string filename = FileUpload1.FileName;
          FileUpload1.PostedFile.Equals(Server.MapPath("~\\Uploads\\" + filename));
          string path = "~\\Uploads\\" + filename;

       SqlConnection cnn = new SqlConnection(connectionString);
        try
        {
            cnn.Open();
            SqlCommand cmd = new SqlCommand();
            cmd.CommandText = "delete from emp ";
            
            cmd.CommandType = CommandType.Text;
            cmd.Connection = cnn;
            cmd.ExecuteNonQuery();

          if (File.Exists(path))
            {
                File.Delete(path);
                 
            }

           
            cnn.Close();
        }
        catch (Exception ex)
        {
            Response.Write(ex.ToString());
        }
        finally
        {
           lblinfo2.Text = "";
           lblinfo2.Text= " Deleted Successfully ";
        }

    }
Tags: C#, ASP.NET

Plain Text
ASM
ASP
ASP.NET
BASIC
BAT
C#
C++
COBOL
CoffeeScript
CSS
Dart
dbase
F#
FORTRAN
HTML
Java
Javascript
Kotlin
Lua
MIDL
MSIL
ObjectiveC
Pascal
PERL
PHP
PowerShell
Python
Razor
Ruby
Scala
Shell
SLN
SQL
Swift
T4
Terminal
TypeScript
VB
VBScript
XML
YAML

Preview



When answering a question please:
  1. Read the question carefully.
  2. Understand that English isn't everyone's first language so be lenient of bad spelling and grammar.
  3. If a question is poorly phrased then either ask for clarification, ignore it, or edit the question and fix the problem. Insults are not welcome.
  4. Don't tell someone to read the manual. Chances are they have and don't get it. Provide an answer or move on to the next question.
Let's work to help developers, not make them feel stupid.
Please note that all posts will be submitted under the http://www.codeproject.com/info/cpol10.aspx.



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900