Solved

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

Posted on 2014-11-20
6
613 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 35

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 35

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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 

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 35

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

Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

Question has a verified solution.

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

How to remove superseded packages in windows w60 or w61 installation media (.wim) or online system to prevent unnecessary space. w60 means Windows Vista or Windows Server 2008. w61 means Windows 7 or Windows Server 2008 R2. There are various …
The purpose of this article is to demonstrate how we can use conditional statements using Python.
The viewer will learn how to implement Singleton Design Pattern in Java.
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

813 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

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now