Solved

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

Posted on 2014-11-20
6
678 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 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
The Ultimate Checklist to Optimize Your Website

Websites are getting bigger and complicated by the day. Video, images, custom fonts are all great for showcasing your product/service. But the price to pay in terms of reduced page load times and ultimately, decreased sales, can lead to some difficult decisions about what to cut.

 

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

Creating Instructional Tutorials  

For Any Use & On Any Platform

Contextual Guidance at the moment of need helps your employees/users adopt software o& achieve even the most complex tasks instantly. Boost knowledge retention, software adoption & employee engagement with easy solution.

Question has a verified solution.

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

The purpose of this article is to demonstrate how we can use conditional statements using Python.
This article shows how to deploy dynamic backgrounds to computers depending on the aspect ratio of display
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
The viewer will learn additional member functions of the vector class. Specifically, the capacity and swap member functions will be introduced.

688 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