You cannot always rely on the browser language as a valid indicator for what currency your visitors use, and sometimes it doesn’t matter because your web application let’s them decide the currency themselves.  You then need a little workaround in order to be able to prefix the right currency symbol to a currency number.

If you format a number like this {0:C} for currency, then .NET automatically uses the current culture as an indicator for choosing the currency symbol to prefix the number like this:  $40,00. But if you want to be able to specify the currency yourself, the you have to tell the threads current culture what the currency symbol should be.

This is examples of what I want to be able to write out without interfering with the users language settings and dependant or their choice of currency:

$ 40,00
£ 40,00
€ 40,00
DKK 40,00
SEK 40,00

Example

It is not that obvious or well documented how to do it, but after some Googling  I was finally able to figure it out. You have to add the correct currency symbol to the threads current culture.  These small methods was what I came up with:

private void SetCurrencyFormat()
{
   string currency = "USD";

   // Clone the current culture
   CultureInfo culture = CultureInfo.CurrentCulture.Clone() as CultureInfo;
   NumberFormatInfo LocalFormat = (NumberFormatInfo)NumberFormatInfo.CurrentInfo.Clone();

   // Set the currency to the culture
   LocalFormat.CurrencySymbol = GetCurrencySymbol(currency);
   culture.NumberFormat = LocalFormat;

   // Add the culture to the current thread
   Thread.CurrentThread.CurrentCulture = culture;
}

private string GetCurrencySymbol(string currency)
{
   switch (currency.ToUpperInvariant())
   {
      case "USD":
         return "$";
      case "EUR":
         return "€";
      case "GBP":
         return "£";
      default:
         return currency;
   }
}

This approach works from ASP.NET and Windows Forms and just about anywhere in the .NET Framework.
Read more about number formats on MSDN.

Isolated Storage is a place on the disk where your .NET application always has write permissions. That makes it ideal for applications that need to store information such as settings or XML without first asking for permission. Application types such as ClickOnce or non-installable applications would benefit hugely from this capability.

It is a little cumbersome to use, because you do not have directly IO access to the Isolated Storage folder. Instead you have to use an IsolatedStorageFileStream and that is a little more complicated than simple file IO. That’s why I always use a static helper class that does the dirty work for me. I use it mainly to write/read applications settings or as data storage for XML files.

This is the class:

#region Using

 

using System.IO;

using System.IO.IsolatedStorage;

 

#endregion

 

namespace Google_Positioner

{

  public static class StorageHelper

  {

 

    /// <summary>

    /// Saves the content to the file.

    /// </summary>s

    public static void Save(string fileName, string content)

    {

      using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))

      {

        using (StreamWriter writer = new StreamWriter(new IsolatedStorageFileStream(fileName, FileMode.Create, isoStore)))

        {

          writer.Write(content);

        }

      }

    }

 

    /// <summary>

    /// Loads the content of the file.

    /// </summary>

    public static string Load(string fileName)

    {

      using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))

      {

        if (isoStore.GetFileNames(fileName).Length > 0)

        {

          using (StreamReader reader = new StreamReader(new IsolatedStorageFileStream(fileName, FileMode.OpenOrCreate, isoStore)))

          {

            return reader.ReadToEnd();

          }

        }

      }

 

      return null;

    }

 

    /// <summary>

    /// Deletes the file to clear the settings.

    /// </summary>

    public static void Delete(string fileName)

    {

      using (IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null))

      {

        if (isoStore.GetFileNames(fileName).Length > 0)

          isoStore.DeleteFile(fileName);

      }

    }

 

  }

}

Example

You can use it to write XML files like I do in this example:

protected override void DataInsert()

{

  StringBuilder sb = new StringBuilder();

  using (XmlWriter writer = XmlWriter.Create(sb))

  {

    writer.WriteStartDocument(true);

    writer.WriteStartElement("Webpage");

    writer.WriteElementString("Name", this.Name);

    writer.WriteElementString("Url", this.Url.ToString());

    writer.WriteEndElement();

  }

 

  StorageHelper.Save(FileName, sb.ToString());

}

And to retrieve the XML file, you can do it like so:

>

protected XmlDocument GetXmlFile()

{

  string content = StorageHelper.Load(FileName);

  if (!string.IsNullOrEmpty(content))

  {

    XmlDocument doc = new XmlDocument();

    doc.LoadXml(content);

 

    return doc;

  }

}