Solved

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

Posted on 2014-11-20
6
580 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
Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

 

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:Tchuki
Tchuki 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

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Suggested Solutions

This article will show, step by step, how to integrate R code into a R Sweave document
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.
The goal of the tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…

760 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

23 Experts available now in Live!

Get 1:1 Help Now