改变文件扩展使用C# [英] Change File Extension Using C#
问题描述
我有很多的文件类型:PDF,TIFF,JPEG,BMP。等等
我的问题是如何改变文件扩展名?
我想这样的:
I have many file types: pdf, tiff, jpeg, bmp. etc. My question is how can I change file extension? I tried this:
my file= c:/my documents/my images/cars/a.jpg;
string extension = Path.GetExtension(myffile);
myfile.replace(extension,".Jpeg");
不管它是什么类型的文件,我指定的格式必须与文件名。但是,这是行不通的。我从浏览器中的文件路径,比如 C:\\ .. \\ .. \\ A.JPG
,文件格式为 a.jpeg
。所以,当我尝试删除它,它给了我一个错误:找不到指定的路径上的文件
。所以,我想它是与文件扩展名不匹配。所以,我试图转换 .JPG
到 .JPEG
并删除文件即可。
No matter what type of file it is, the format I specify must be with the file name. But it does not work. I get file path from browser like c:\..\..\a.jpg
, and the file format is a.jpeg
. So, when I try to delete it, it gives me an error: Cannot find the file on specified path'
. So, I am thinking it has something to do with the file extension that does not match. So, I am trying to convert .jpg
to .jpeg
and delete the file then.
推荐答案
有:<一href=\"http://msdn.microsoft.com/en-us/library/system.io.path.changeextension.aspx\"><$c$c>Path.ChangeExtension$c$c>方法。例如:
There is: Path.ChangeExtension
method. E.g.:
var result = Path.ChangeExtension(myffile, ".jpg");
在这种情况下,如果你也想改变身体的延伸,你可以使用的 File.Move
方式:
File.Move(myffile, Path.ChangeExtension(myffile, ".jpg"));
这篇关于改变文件扩展使用C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!