Visual C# Developer Center > Visual C# Forums > Visual C# Language > How to save text file in MS-DOS Format?
Ask a questionAsk a question
 

AnswerHow to save text file in MS-DOS Format?

  • Thursday, May 07, 2009 6:47 AMLunar Fifth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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.

Answers

All Replies

  • Thursday, May 07, 2009 6:58 AMM. Dirksma Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     Has Code
    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);
  • Thursday, May 07, 2009 7:49 AMLunar Fifth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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?
  • Thursday, May 07, 2009 7:54 AMM. Dirksma Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Try the code listed here to found out what code pages u have installed:
    http://msdn.microsoft.com/en-us/library/system.text.encodinginfo.aspx

    Im not sure if u need to install some additional software to support chinese characters.
  • Thursday, May 07, 2009 8:08 AMLunar Fifth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    Try the code listed here to found out what code pages u have installed:
    http://msdn.microsoft.com/en-us/library/system.text.encodinginfo.aspx

    Im 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.
  • Thursday, May 07, 2009 8:25 AMHan Cheng Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Thursday, May 07, 2009 9:39 AMLunar Fifth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Wednesday, May 13, 2009 7:53 AMHarry ZhuMSFT, ModeratorUsers MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     AnswerHas Code
    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.
  • Sunday, May 31, 2009 7:35 AMLunar Fifth Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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.
  • Wednesday, August 19, 2009 10:36 PMkaroshi1076 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    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"
  • Friday, November 06, 2009 7:19 PMStan1826 Users MedalsUsers MedalsUsers MedalsUsers MedalsUsers Medals
     
    karoshi1076,

    Thanks for sharing your code... It works great.  Do you have any idea if the HEW program can be automated?