Dear Friends,

In my C# desktop application, I get the startup path from Application.StartupPath.Then my program creates a batch file, which changes the directory (with cd) to the above startup path (since sometimes it runs batches which changes the current directory).
In English windows it works great. But on Spanish Windows, my app's startuppath contains accent. So when I view the batch file I created, it has invalid characters where accent was before.
I create the batch file in the following way:

StreamWriter sw = new StreamWriter("a.bat"); sw.WriteLine("cd " + Application.StartupPath); sw.Close();

As I described, in a.bat I'll find that path corrupted where the accent originally appears.
Note that in MSDN documentation, it says that StreamWriter always uses UTF-8.

How can I avoid this problem?

Please help, it's very urgent.

Thanks deeply,

9 Years
Discussion Span
Last Post by sknake

You can specify the encoding used in the streamwriter

new StreamWriter(@"C:\path\", false, Encoding.Default);

Thanks much for the fast reply. But it didn't work.
I had a mistake in my last post, and I used:

StreamWriter sw = File.CreateText("p.bat");

But, neither the following two works:

StreamWriter sw = new StreamWriter("p.bat", false, Encoding.Default);
StreamWriter sw = new StreamWriter("p.bat", false, Encoding.UTF8);

In the batch there are still garbage characters where chars with accent should be.

Please help, and many many thanks.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.