如何在ASP.NET中重命名文件?

时间:2022-10-03 10:48:15

In my project I want to rename the file before it is updating. For example a file in my system like Mycontact.xls. I want to rename it as sasi.xls (it is an excel file). How can I write the code in ASP.NET?

在我的项目中,我想在文件更新之前重命名该文件。例如,我的系统中的文件,如Mycontact.xls。我想将其重命名为sasi.xls(它是一个excel文件)。如何在ASP.NET中编写代码?

Actually I am using a fileupload control to get the file in and rename the file and upload the renamed file in a folder which is in Solution Explorer.

实际上我正在使用fileupload控件来获取文件并重命名文件,并将重命名的文件上传到解决方案资源管理器中的文件夹中。

3 个解决方案

#1


8  

C# does not provide a file rename function, unfortunately. Anyhow, the idea is to do this:

遗憾的是,C#不提供文件重命名功能。无论如何,这个想法是这样做的:

File.Copy(oldFileName, NewFileName);

File.Delete(oldFileName);

You can also use - File.Move.

你也可以使用 - File.Move。

#2


9  

You can do it with the File.Move method eg:

您可以使用File.Move方法执行此操作,例如:

string oldFileName = "MyOldFile.txt";
string newFileName = "MyNewFile.txt";
File.Move(oldFileName, newFileName);

#3


0  

Be aware that when that code executes, the owner of the file will turn into the identity you have set on your Application Pool on which the website is running.

请注意,当该代码执行时,该文件的所有者将变为您在运行该网站的应用程序池上设置的标识。

That account might not have enough permissions to 'create new' or 'delete' files.

该帐户可能没有足够的权限来“创建新”或“删除”文件。

I would advise you to place all read/writable files in a seperate location so you can control the security settings seperately on that part. This will also split off the 'only readable files/executables' (like the aspx and such) from the 'read/writable' files.

我建议您将所有可读/写文件放在一个单独的位置,以便您可以单独控制该部分的安全设置。这也将从'可读/可写'文件中分离出“只有可读文件/可执行文件”(如aspx等)。

#1


8  

C# does not provide a file rename function, unfortunately. Anyhow, the idea is to do this:

遗憾的是,C#不提供文件重命名功能。无论如何,这个想法是这样做的:

File.Copy(oldFileName, NewFileName);

File.Delete(oldFileName);

You can also use - File.Move.

你也可以使用 - File.Move。

#2


9  

You can do it with the File.Move method eg:

您可以使用File.Move方法执行此操作,例如:

string oldFileName = "MyOldFile.txt";
string newFileName = "MyNewFile.txt";
File.Move(oldFileName, newFileName);

#3


0  

Be aware that when that code executes, the owner of the file will turn into the identity you have set on your Application Pool on which the website is running.

请注意,当该代码执行时,该文件的所有者将变为您在运行该网站的应用程序池上设置的标识。

That account might not have enough permissions to 'create new' or 'delete' files.

该帐户可能没有足够的权限来“创建新”或“删除”文件。

I would advise you to place all read/writable files in a seperate location so you can control the security settings seperately on that part. This will also split off the 'only readable files/executables' (like the aspx and such) from the 'read/writable' files.

我建议您将所有可读/写文件放在一个单独的位置,以便您可以单独控制该部分的安全设置。这也将从'可读/可写'文件中分离出“只有可读文件/可执行文件”(如aspx等)。