Solved

How to change the file extension to upper case in c#?

Posted on 2014-11-20
6
640 Views
Last Modified: 2014-11-24
I am generating PDF Files with the extension "pdf" in small letters and requirement is to change it to PDF (All in upper case).  When I generate the files and debug, it shows the intended output and after generating it again displays the same lower case extension.
I have 34 files in the folder and it does change the file for only three files and rest of them lower case.
Can anyone please guide me how to resolve this issue?
here is my code:
 FormName = "Form" + form + ".PDF";
fsOutput = new FileStream(TemplatePath + FormName, FileMode.Create,                                                  FileAccess.Write);
objDoc.Generate(fsoutput);

Open in new window

0
Comment
Question by:developer2012
  • 3
  • 2
6 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 250 total points
ID: 40456131
Two alternatives:
A1. use an invariant string:
FormName = String.Format(CultureInfo.CurrentCulture, "Form{0}.PDF", form);

Open in new window

A2. Use the ToUpperInvariantmethod
string[] files = Directory.GetFiles("your dir", "*.pdf");
foreach(string file in files)
{        
    System.IO.File.Move(file, file.ToLowerInvariant());
}

Open in new window

Note: 1) I have not tested yet as I do not have VS handy.
2) From MSDN:
If you need the lowercase or uppercase version of an operating system identifier, such as a file name, named pipe, or registry key, use the ToLowerInvariant or ToUpperInvariant methods.
0
 

Author Comment

by:developer2012
ID: 40457965
The first one works for me but it displays the string as "FormForm12345.PDF" and If I modify the code to resolve it does not work
0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40458473
What exactly do you need?
e.g. if  form="Form12345" then FormName=  "FormForm12345.PDF" as per original code.
if you are trying to avoid duplicating word "form" just modify the format string like:
FormName = String.Format(CultureInfo.CurrentCulture, "{0}.PDF", form);

Open in new window

thus, FormName=  "Form12345.PDF"
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:developer2012
ID: 40458480
I did modify the string the same way you did and it returned me 1234.PDF as file name
0
 
LVL 14

Assisted Solution

by:Vel Eous
Vel Eous earned 250 total points
ID: 40459398
If you only want to uppercase the file extension, perhaps the following will work for you.

var formName = "Form" + form + ".PDF";
var fsOutput = new FileStream(formName, FileMode.Create, FileAccess.Write);
objDoc.Generate(fsOutput);
// close the filestream before we can work on the file it created
fsOutput.Close();
File.Move(formName, Path.ChangeExtension(formName, Path.GetExtension(formName).ToUpper())); 

Open in new window

0
 
LVL 36

Expert Comment

by:Miguel Oz
ID: 40460111
What is the value of the form variable?
It seems that in your last example form=1234, if that is the case you need to use my original code.
My original String.Format code is doing two jobs:
- Creating an invariant name that you can use for your filename.
- Adding a prefix "Form" and a suffix ".PDF" to your form variable by using the format string ("Form{0}.PDF" )
I proposed that solution because the original code was (FormName = "Form" + form + ".PDF";), if your needs have changed please feel free to modify  the format string to your current needs.
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

A long time ago (May 2011), I have written an article showing you how to create a DLL using Visual Studio 2005 to be hosted in SQL Server 2005. That was valid at that time and it is still valid if you are still using these versions. You can still re…
Entity Framework is a powerful tool to help you interact with the DataBase but still doesn't help much when we have a Stored Procedure that returns more than one resultset. The solution takes some of out-of-the-box thinking; read on!
This video teaches viewers about errors in exception handling.
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…

749 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question