想要在上传新文件时自动删除以前的文件 [英] Want to delete the previous file automatically when new file is uploaded

查看:146
本文介绍了想要在上传新文件时自动删除以前的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个上传者: -



 <  < span class =code-leadattribute> div     style   =  width:250px; margin:0px 0px 0px 150px; float:right; > ;  

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

< asp:按钮 ID = Button2 runat = server onclick = Button2_Click

文字 = 保存文件 / >

< / div >







现在我想当我上传另一个文件时,先前的文件会被自动删除,这个新文件会被上传而不是旧文件。



这是我的代码(我只能制作上传代码...... :-()



string str = @data source = DEEPAKSHARMA-PC\SQLEXPRESS;初始目录=新; 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();

解决方案

使用此



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





要么发送路径静态,要么填充它动态数据库。

:)


使用..

  string  rootFolderPath =  @  C: \\SomeFolder\\AnotherFolder\\FolderCOntainingThingsToDelete; 
string filesToDelete = @ * DeleteMe * .DOC; // 仅删除文件名中包含DeleteMe的DOC文件
string [] fileList = System.IO.Directory.GetFiles(rootFolderPath,filesToDelete);
foreach 字符串文件 in fileList)
{
System.Diagnostics.Debug.WriteLine(file + 将被删除);
// System.IO.File.Delete(file);
}



ref。

http://stackoverflow.com/questions/1620366/delete-files-from-directory-if-filename-contains-a-certain-word [< a href =http://stackoverflow.com/questions/1620366/delete-files-from-directory-if-filename-contains-a-certain-word\"target =_ blanktitle =New Window> ^ ]

http://stackoverflow.com/questions/1288718/how-to-delete-all-files-and-folders-in-a-directory [ ^ ]

< a href =http://stackoverflow.com/questions/14251220/how-to-delete-a-specific-file-from-folder-u sing-asp-net> http://stackoverflow.com/questions/14251220/how-to-delete-a-specific-file-from-folder-using-asp-net [ ^


I have this uploader:-

<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();

解决方案

Use this

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.
:)


use..

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[^]


这篇关于想要在上传新文件时自动删除以前的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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