首页 > 解决方案 > 在 C# 中上传到 SQL Server 之前重命名文件名?

问题描述

在将文件上传到 SQL Server 之前,如何在 C# 中重命名文件?

我试过这个:

if (n.HasFile)
{
    string fileNameWithoutExtension=Path .GetFileNameWithoutExtension(CapitalStructureShareHoldingFile.FileName);
    string extension = Path.GetExtension(n.FileName);
    fileNameWithoutExtension = "n" + extension;
}

对于这个文件,我想将它重命名为n它的扩展名。

标签: c#sql-serverrename

解决方案


if (ASPxUploadControl1.HasFile ) or//(Fileupload.Hasfile)
 {
     try
     {
         string extension = Path.GetExtension(ASPxUploadControl1.FileName);
         string id = Guid.NewGuid().ToString();  //here give your username or whatever you want
         string fileLocation = string.Format("{0}/{1}{2}", Server.MapPath("upload/"), id, extension);
         ASPxUploadControl1.SaveAs( fileLocation );
         StatusLabel.Text = "Upload status: File uploaded!";
     }
     catch (Exception ex)
     {
         StatusLabel.Text = "Upload status: The file could not be uploaded. The following error occured: " + ex.Message;
     }
 }

推荐阅读