如何在c#中重命名文件 [英] How can I rename files in c#

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

问题描述

你好



我想知道c#中的重命名是如何工作的

例如,我想选择文件夹,它应该重命名*。 dat to * .bmp

此外,对于所选文件。



虽然在





Hello

I wanna know how exactly rename in c# works
For example , I wanna select folder and it should rename *.dat to *.bmp
Also , for the selected file .

Though in


File.Copy(textBox1.text, textBox2.text);

File.Delete(textBox1.text);



其中textBox1.text有浏览文件的方法,它会写入路径那里

其中textBox2.Text可以输入你的新类型示例:* .png * .mp3等



但它不会工作100%:eek:



那么,我可以得到帮助吗?:o


Where textBox1.text has the method for browse a file , and it will write the path there
Where textBox2.Text has the place to type your new type example: *.png *.mp3 etc.

but It wont work 100% :eek:

So , can I get help?:o

推荐答案

使用 File.Move(sourceFilePath,destFilePath)

如果文件路径的唯一区别是名称,那么文件将被重命名,而不是重定位。

确保 destFilePath 不存在,如 File.Move()不会替换现有文件。

(当然,标准的权限问题也适用。)
Use File.Move(sourceFilePath, destFilePath)
If the only difference in the file paths is the name, then the file will just be renamed, not relocated.
Be sure the destFilePath doesn''t already exist, as File.Move() will not replace an existing file.
(And, of course, standard issues of permissions apply.)


改为使用move。

http://msdn.microsoft.com/en-us/library/system.io.file.move。 aspx [ ^ ]



这样就可以移动文件而不是经历重新创建和删除的开销。
Use move instead.
http://msdn.microsoft.com/en-us/library/system.io.file.move.aspx[^]

That way the file is moved instead of going through the overhead of recreating and deleting.


重命名一个文件只是覆盖相同的文件名。你可以使用相同的File.Copy方法实现它,bool标志为true作为第三个参数。

Rename a file is nothing but the overwriting to the same file name. You can achieve that using the same File.Copy method with bool flag true as third parameter.
File.Copy(textBox1.text, textBox2.text,true);


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

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