PC Review


Reply
Thread Tools Rate Thread

Creating file name form string

 
 
Andrus
Guest
Posts: n/a
 
      28th Jun 2008
I need to create legal file name form any string so that PDF and other
viewers show nice title.

I created method LegalFileName() which uses 2 helper methods.
It replaces <>\/:?*"| characters in name.
Is this best solution ?

Andrus.

/// <summary>
/// Creates valid file name for current OS
/// </summary>
/// <returns>Legal file name</returns>
public static string LegalFileName(string proposedFileName)
{
return ChrTran(proposedFileName, "<>\\/:?*\"|", "[] ").Trim();
}

/// <summary>
/// Replaces each character in a character expression that matches a
character
/// in a second character expression with the corresponding character in
a
/// third character expression
/// </summary>
/// <example>
/// Console.WriteLine(ChrTran("ABCDEF", "ACE", "XYZ")); //Displays
XBYDZF
/// Console.WriteLine(ChrTran("ABCD", "ABC", "YZ")); //Displays YZD
/// Console.WriteLine(ChrTran("ABCDEF", "ACE", "XYZQRST")); //Displays
XBYDZF
/// </example>
/// <param name="cSearchIn"> </param>
/// <param name="cSearchFor"> </param>
/// <param name="cReplaceWith"> </param>
public static string ChrTran(string cSearchIn, string cSearchFor, string
cReplaceWith)
{
string lcRetVal = cSearchIn;
string cReplaceChar;
for (int i = 0; i < cSearchFor.Length; i++)
{
if (cReplaceWith.Length <= i)
cReplaceChar = "";
else
cReplaceChar = cReplaceWith[i].ToString();

lcRetVal = StrTran(lcRetVal, cSearchFor[i].ToString(),
cReplaceChar);
}
return lcRetVal;
}


/// <summary>
/// Searches one string into another string and replaces all occurences
with
/// a third string.
/// <pre>
/// Example:
/// StrTran("Joe Doe", "o", "ak"); //returns "Jake Dake"
/// </pre>
/// </summary>
/// <param name="cSearchIn"> </param>
/// <param name="cSearchFor"> </param>
/// <param name="cReplaceWith"> </param>
public static string StrTran(string cSearchIn, string cSearchFor, string
cReplaceWith)
{
//Create the StringBuilder
StringBuilder sb = new StringBuilder(cSearchIn);

//There is a bug in the replace method of the StringBuilder
sb.Replace(cSearchFor, cReplaceWith);

//Call the Replace() method of the StringBuilder and specify the
string to replace with
return sb.Replace(cSearchFor, cReplaceWith).ToString();
}

 
Reply With Quote
 
 
 
 
cfps.Christian
Guest
Posts: n/a
 
      30th Jun 2008
That should work, you can also do the wrong thing by putting a try/
catch block around the creation to catch any errors and bubble it back
up.

 
Reply With Quote
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Creating a form object from string name MikeB Microsoft VB .NET 9 19th Nov 2006 06:21 AM
How can I read an XML string directly into a dataset without creating a file? Don Microsoft VB .NET 2 8th Feb 2006 07:00 AM
VBN2002 - Programmatically creating a form using a string value as name Michael Creager Microsoft VB .NET 2 7th Apr 2005 01:21 AM
Creating a Custom Help File for a Form Eric Microsoft Access Forms 4 26th Nov 2003 10:28 AM
creating backup file from a form Bill Microsoft Access Forms 1 10th Oct 2003 02:24 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 08:46 PM.