?
Solved

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

Posted on 2014-11-20
6
Medium Priority
?
711 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 36

Accepted Solution

by:
Miguel Oz earned 750 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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 750 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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

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 …
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
The viewer will learn how to implement Singleton Design Pattern in Java.
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Suggested Courses

762 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