title: Edit a text file in c#, the file happens to be an Illustrator (.ai) file
body: <p>We have hundreds of Illustrator files that use a font that needs to be changed.  I can open the Illustrator file in Notepad++, search for the font in question and replace it with the new font.  The file then opens without issue in Illustrator using the correct font.</p>
  <p>I'm trying to write a bit of C# code to do this automatically, but Illustrator claims that the resulting file is corrupted.</p>
  <p>I assumed this was due to the encoding of the file, so I've tried reading and writing the file with various different encodings and the result is basically the same.  The file either won't open at all, or it is blank white page.  Either way, Illustrator says the file is corrupted.</p>
  <p>After several revisions, here is my code as it stands right now:</p>
  <pre><code>public void RenameFont()
  {
      DirectoryInfo directory = new DirectoryInfo(Common.SOURCE_FOLDER);
      FileInfo[] files = directory.GetFiles(&quot;*.ai&quot;, SearchOption.TopDirectoryOnly);
      foreach (FileInfo file in files)
      {
          string fileName = file.Name.Replace(&quot;.ai&quot;, &quot;&quot;);
          if (int.TryParse(fileName, out int itemNumber))
          {
              using (FileStream fs = file.OpenRead())
              using (StreamReader reader = new StreamReader(fs, Encoding.GetEncoding(1252)))
              {
                  string line;
                          
                  using (StreamWriter writer = new StreamWriter(File.Open(Path.Combine(file.DirectoryName, $&quot;new{file.Name}&quot;), FileMode.Create), Encoding.GetEncoding(1252)))
                  {
                      while ((line = reader.ReadLine()) != null)
                      {
                          foreach (string badFont in Common.SEARCH_FOR)
                          {   
                              if (line.Contains(badFont))
                              {
                                  line = line.Replace(badFont, Common.REPLACE_WITH);
                              }
                          }
                          writer.WriteLine(line);
                      }
                      writer.Flush();
                      writer.Close();
                  }
                  reader.Close();
              }
          }
      }
  }
  </code></pre>
  <p>Even if I just read and write the file without modifying the font name, the result is still the same.</p>
  <p>EDIT:  I wound up using Notepad++'s &quot;Find in Files&quot; feature to replace the text.  It handled all the files in less than a minute and they all open in Illustrator without issue.</p>