Le réseau pour les développeurs >
Forums - Accueil
>
Visual C# Language
>
How to save text file in MS-DOS Format?
How to save text file in MS-DOS Format?
- I had a program to read a text file (dos fmt) in, then to do some processing, then to save in another file. But I always got resulting file in UNIX fmt.
here's how I read file in,
if ((myStream = openFileDialog1.OpenFile()) != null) { using (myStream) { m_openedFilePath = openFileDialog1.FileName; m_editor.Text = File.ReadAllText(m_openedFilePath, Encoding.Default); }and how I write to a new file,
if (targetFile != string.Empty) { try { File.WriteAllText(targetFile, m_editor.Text, Encoding.Default); } catch (Exception ex) { MessageBox.Show(ex.Message); } }
How to solve the problem?
Thanks.
Réponses
- Hi,
You might need to split the content of m_editor.Text to lines , and write them to the new file seperated with :Environment.NewLine.
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marqué comme réponseHarry ZhuMSFT, Moderatorvendredi 15 mai 2009 02:02
Toutes les réponses
- Try this one when u are reading the file:Encoding dosEnc = Encoding.GetEncoding("437"); // ms-dos codepage ( US English )Then when reading the file, use: m_editor.Text = File.ReadAllText(m_openFilePath, dosEnc);
- Try this one when u are reading the file:Encoding dosEnc = Encoding.GetEncoding("437"); // ms-dos codepage ( US English )Then when reading the file, use: m_editor.Text = File.ReadAllText(m_openFilePath, dosEnc);
Thanks.
But it didn't work for me.
First, I gotta display some simplified chinese words, so 437 got me confusing display. Then I tried 936, still not worked, waah. I believe 936 is my defualt encoding.
Any other solution? - Try the code listed here to found out what code pages u have installed:http://msdn.microsoft.com/en-us/library/system.text.encodinginfo.aspxIm not sure if u need to install some additional software to support chinese characters.
- Try the code listed here to found out what code pages u have installed:http://msdn.microsoft.com/en-us/library/system.text.encodinginfo.aspxIm not sure if u need to install some additional software to support chinese characters.
I'm running a Simplified Chn Windows, not require additional support.
I think it has nothing to do with encoding. - After reading out the contents, why dun you do a simple system write to see if the contents are properly read out? This will determine if it is an encoding issue.
After reading out the contents, why dun you do a simple system write to see if the contents are properly read out? This will determine if it is an encoding issue.
I've tried several encodings, UTF8, UNICODE, DEFAULT (ANSI 936), each of them worked fine for reading and writing except the file format was changed to UNIX fmt.
Theoritically, encodings just matter how characters get correctly displayed or not. But ending character of line is different from DOS and UNIX file fmt.
I'm looking for a simple solution to fix it.- Hi,
You might need to split the content of m_editor.Text to lines , and write them to the new file seperated with :Environment.NewLine.
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.- Marqué comme réponseHarry ZhuMSFT, Moderatorvendredi 15 mai 2009 02:02
Hi,
You might need to split the content of m_editor.Text to lines , and write them to the new file seperated with :Environment.NewLine.
Harry
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
Welcome to the All-In-One Code Framework! If you have any feedback, please tell us.
thanks. it worked.
Actually, Environment.NewLine was not required, I just fed m_editor.Lines to File.WriteAllLines, thans. Sorry for my late reply.- I signed up just to reply. Thanks for the post this helped me out a lot. I was using this for the new HIPAA Eligibility program and here's my final code which solved the problem.
string path = @"C:\Dan\Response2.txt" ;
string [] myfile = File .ReadAllText(path,System.Text.Encoding .Default).Split(Environment .NewLine.ToCharArray());
File .WriteAllLines(path,myfile);
With this, the HEW program reads the file without the user having to open it in wordpad and save as "Text File - MS DOS Format"
- karoshi1076,
Thanks for sharing your code... It works great. Do you have any idea if the HEW program can be automated?

