重命名C#中的文件 [英] Renaming a File in C#

查看:160
本文介绍了重命名C#中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的朋友,

我为用户提供了一个上传文件的选项.
他/她将在何处上传任何名称的文件.
但是在将其存储在文件夹中时,我必须使用相应的用户ID重命名该文件.
我想用C#做到这一点,请指导我.

Dear Friends,

I have provided a option for the user to upload a file.
Where as he/she will be uploading the file with any name.
But while storing it in a folder I have to rename the file with respective User''s ID.
I want to do this in C#, Kindly guide me.

推荐答案


这很容易做到.上载文件后,获取文件扩展名并将其另存为所需名称.请检查下面的示例代码.让Uploader作为您的文件上传控件ID.

Hi,
It is very easy to do. When the file is uploaded, get the extension of the file and save it as with your desired name. Please check the sample code below. Let Uploader be your fileupload control ID.

if (Uploader.HasFile)
        {
            string extension = Path.GetExtension(Uploader.PostedFile.FileName);
            Uploader.SaveAs(uploadFolder + "NewFileName"+ extension);
            labelfile.Text = "File uploaded successfully as: " + "NewFileName"+ extension;
        }




希望对您有帮助..编码愉快..:)




Hope it helps.. Happy coding.. :)


您可以在上传到文件夹中时实现此功能-


这样,您可以使文件名唯一,并使用用户名

Hi you can achieve this while uploadin to your folder -


by this you can make your file name unique and with your username

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;
     }
 }




使用下面的链接...

http://www.c- sharpcorner.com/blogs/4386/rename-file-and-folder-in-c-sharp-asp-net.aspx [
Hi,

Use the below link...

http://www.c-sharpcorner.com/blogs/4386/rename-file-and-folder-in-c-sharp-asp-net.aspx[^]


这篇关于重命名C#中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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