| |

VerySource

 Forgot password?
 Register
Search
View: 756|Reply: 3

How to write a string to a file in ANSI format

[Copy link]

1

Threads

1

Posts

2.00

Credits

Newbie

Rank: 1

Credits
2.00

 China

Post time: 2020-1-28 15:40:02
| Show all posts |Read mode
I want to write a small tool in C # to construct some VBScript, nsi files, but I found that the file format is UTF-8 (part of the content is C # program to judge the construction of the string itself, and the other part is prepared in advance Read out the files, those file formats are ANSI). Now I find that the Chinese in the generated files have become garbled, and even the 'sign behind the Chinese string disappears, causing vbscript to be unusable.

Is there any way to make the generated file also ansi format and guarantee that no garbled characters appear?

The method of copying files and writing files (that is, generating new files by these two methods):
 1. Copy file
        public static string CopyFile (string srcFileName, string aimFileName)
        {
            string result = "";
            StreamReader SR = null;
            try
            {
                string S;
                SR = new StreamReader (srcFileName, Encoding.Default); // File.OpenText (srcFileName);
                S = SR.ReadLine ();
                while (S! = null)
                {
                    AppendToFile (aimFileName, S);
                    //Console.WriteLine(S);
                    S = SR.ReadLine ();
                }
            }
            catch (Exception e)
            {
                string s = e.ToString ();
                throw;
            }
            finally
            {
                if (SR! = null)
                    SR.Close ();
            }
            return (result);
        }
  2. Write file
         public static bool AppendToFile (string filepath, string comment)
        {
            StreamWriter SW;
            SW = File.AppendText (filepath);
            SW.WriteLine (comment);
            SW.Close ();
            return true;
        }
Reply

Use magic Report

2

Threads

29

Posts

16.00

Credits

Newbie

Rank: 1

Credits
16.00

 China

Post time: 2020-3-10 20:45:02
| Show all posts
Try the ASCIIEncoding class
Reply

Use magic Report

0

Threads

8

Posts

8.00

Credits

Newbie

Rank: 1

Credits
8.00

 China

Post time: 2020-3-14 19:45:01
| Show all posts
Encoding class. Upstairs is fine too. StreamWriter has a constructor that requires an Encoding parameter. You specify ASCII
Reply

Use magic Report

0

Threads

38

Posts

22.00

Credits

Newbie

Rank: 1

Credits
22.00

 China

Post time: 2020-3-15 12:00:01
| Show all posts
StreamWriter sw = new StreamWriter (path, System.Encoding.Default);
Reply

Use magic Report

You have to log in before you can reply Login | Register

Points Rules

Contact us|Archive|Mobile|CopyRight © 2008-2023|verysource.com ( 京ICP备17048824号-1 )

Quick Reply To Top Return to the list