Change File Extension Using C#

后端 未结 5 1473
北荒
北荒 2020-11-29 06:34

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.jp         


        
5条回答
  •  感动是毒
    2020-11-29 06:50

    You should do a move of the file to rename it. In your example code you are only changing the string, not the file:

    myfile= "c:/my documents/my images/cars/a.jpg";
    string extension = Path.GetExtension(myffile); 
    myfile.replace(extension,".Jpeg");
    

    you are only changing myfile (which is a string). To move the actual file, you should do

    FileInfo f = new FileInfo(myfile);
    f.MoveTo(Path.ChangeExtension(myfile, ".Jpeg"));
    

    See FileInfo.MoveTo

提交回复
热议问题