0
我需要在c#中用現有文件替換文件並刪除原始文件。 這是我當前的代碼,這代碼不具有相同名稱替換文件:如何用現有文件替換文件並刪除原文件?
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
PopulateUploadedFiles();
}
}
private void PopulateUploadedFiles()
{
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
List<UploadedFile> allFiles = dc.UploadedFiles.ToList();
DataListOktobar.DataSource = allFiles;
DataListOktobar.DataBind();
}
}
protected void BtnUpload_Click(object sender, EventArgs e)
{
if (FileUpload1.HasFile)
{
HttpPostedFile file = FileUpload1.PostedFile;
BinaryReader br = new BinaryReader(file.InputStream);
byte[] buffer = br.ReadBytes(file.ContentLength);
using (MyDatabaseEntities dc = new MyDatabaseEntities())
{
dc.UploadedFiles.Add(
new UploadedFile
{
FileName = file.FileName,
ContentType = file.ContentType,
FileID = 0,
FileSize = file.ContentLength,
FileExtension = Path.GetExtension(file.FileName),
FileContent = buffer
});
dc.SaveChanges();
PopulateUploadedFiles();
感謝您的幫助Martin Staufcik –