1

While learning with books sometimes i copy some code from pdf, to test it.

This tiny exe was suppose to change ‘ ’ “ ” to ' or "

and it work fine, but only if tested *.cs file is opened manually before I debug my methods.

Otherwise it's not working. When code paste into concerned file directly, and closed, without opening once again The Replace method return "Unexpected character ?"

I dont understand the probleme, since File.ReadAllText already open and close the file.

using System;
using System.Collections.Generic;
using System.IO;

class Test
{
    public static void Main()
    {
        string path = Directory.GetCurrentDirectory();

        string[] csfiles = Directory.GetFiles(path, "*.cs");

        foreach (var item in csfiles)
        {
            string text = File.ReadAllText(item);

            text = text.Replace("‘", "\'")
                       .Replace("’", "\'")
                       .Replace("“", "\"")
                       .Replace("”", "\"");

            File.WriteAllText(item, text);
        }
    }
}
Louis
  • 89
  • 1
  • 2
  • 11
  • Just to clarify you run this code against a file like example.cs and it is this file that you need to open before it works (rather than the .cs file of the code you have in the question). Also do you have to have the file open when you run it or do you just need to open and save it again? Also how does the example.cs file get created? – Chris Jan 17 '17 at 22:47
  • example.cs is the file that I want to change. yes. and it has to be reopended so Replace work correctly on this file. it doesnt have to be opened while running replace. I just need to makes changes on the file. example.cs is created mannually by copy and paste some code from book in pdf. – Louis Jan 17 '17 at 22:51
  • Why don't you just use a text editor to do the replacement? Are you sure the file you want to modify is in the working directory when its executing? – GantTheWanderer Jan 17 '17 at 22:55
  • It definitely sounds like when you are first creating the file you are doing somethign strange. How are you creating the file (ie are you copying and pasting it into notepad, are you using a different text editor, something else)? Also if you feel confident with hex editors you could try copying the file before you open it and "fix" it and compare to the original after you have fixed it and see what the differences are in the files in hex which would probably tell us exactly what has gone wrong. – Chris Jan 17 '17 at 23:10

1 Answers1

-1

Apparently File.ReadAllText(); does change encoding when opening.

My caracters (being in ASCII+ANSI) was ruined just when opened.

string text = File.ReadAllText(path, Encoding.Default); keeps original encoding when opening. Replace work fine on this, and so my exe.

:) Thank you for your help!

using System;
using System.Collections.Generic;
using System.IO;
using System.Text;

class Test
{
public static void Main()
{

    string path = Directory.GetCurrentDirectory();

    string[] csfiles = Directory.GetFiles(path, "*.cs");

    foreach (var item in csfiles)
    {
        string text = File.ReadAllText(item, Encoding.Default);

        string newtext = text.Replace("‘", "\'")
                   .Replace("’", "\'")
                   .Replace("“", "\"")
                   .Replace("”", "\"");

        File.WriteAllText(item, newtext);
    }
}
}

How to read an ANSI encoded file containing special characters

Community
  • 1
  • 1
Louis
  • 89
  • 1
  • 2
  • 11