Click here to Skip to main content
16,017,852 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have this uploader:-

XML
<div style="width:250px; margin:0px 0px 0px 150px; float:right;">

            <asp:FileUpload ID="FileUpload1" runat="server" />

            <asp:Button ID="Button2" runat="server" onclick="Button2_Click"
                Text="Save File" />

        </div>



Now I want when I upload another file the previous file gets deleted automatically and this new file is uploaded in place of old one.

This is my code (I can only make code for uploading... :-( )

string str=@"data source=DEEPAKSHARMA-PC\SQLEXPRESS; initial catalog=new; integrated security=true ";
SqlConnection con=new SqlConnection(str);
con.Open();
string s=Server.MapPath("doc/")+f1.FileName;
f1.SaveAs(s);

string quer = "insert into doc values('" + f1.FileName + "')";
SqlCommand cmd = new SqlCommand(quer, con);
cmd.ExecuteNonQuery();
con.Close();
Posted

Use this

C#
string path=Server.MapPath("filefolder/filename.extension");
if(System.IO.File.Exists(path))
{
 System.IO.File.Delete(path);
}


Either send the path static or populate it from database dynamically.
:)
 
Share this answer
 
Comments
Deepak Kanswal Sharma 8-Feb-15 6:56am    
Great dude!! That worked! Thanks a lot. ;)
use..
C#
string rootFolderPath = @"C:\\SomeFolder\\AnotherFolder\\FolderCOntainingThingsToDelete";
string filesToDelete = @"*DeleteMe*.doc";   // Only delete DOC files containing "DeleteMe" in their filenames
string[] fileList = System.IO.Directory.GetFiles(rootFolderPath, filesToDelete);
foreach(string file in fileList)
{
    System.Diagnostics.Debug.WriteLine(file + "will be deleted");
//  System.IO.File.Delete(file);
}

ref.
http://stackoverflow.com/questions/1620366/delete-files-from-directory-if-filename-contains-a-certain-word[^]
http://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory[^]
http://stackoverflow.com/questions/14251220/how-to-delete-a-specific-file-from-folder-using-asp-net[^]
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



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