Try this:
[CommandMethod("CreateHatchPattern")]
public static void CreateHatchPattern()
{
string path = Directory.GetParent(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)).FullName;
if (Environment.OSVersion.Version.Major >= 6)
{
path = Directory.GetParent(path).ToString();
}
//Adjust the Path According to your AutoCad version
//FileName must be equal to pattern name
path = path + "\\AppData\\Roaming\\Autodesk\\AutoCAD 2015\\R20.0\\enu\\Support\\Tile Test.pat";
List<string> Pattern = new List<string>();
//Pattern see https://knowledge.autodesk.com/support/autocad/troubleshooting/caas/sfdcarticles/sfdcarticles/Creating-new-Custom-Hatch-patterns.html
//and https://knowledge.autodesk.com/support/autocad/troubleshooting/caas/sfdcarticles/sfdcarticles/Error-in-pattern-file-in-AutoCAD.html
//FileName must be equal to pattern name
Pattern.Add("*Tile Test");
Pattern.Add("45, 0,0, 0,.125");
Pattern.Add("135, 0,0, .25,.125, .3125,-.1875");
//Last return needed!
Pattern.Add("");
if (!File.Exists(path))
{
File.AppendAllLines(path, Pattern);
}
else if (File.Exists(path))
{
File.Delete(path);
File.AppendAllLines(path, Pattern);
}
}
I did'nt use TextWriter because the .pat file is in use.
There are a few rules to be followed when creating custom .pat patterns:
See:
https://knowledge.autodesk.com/support/autocad/troubleshooting/caas/sfdcarticles/sfdcarticles/Error-...
PS. I suffered from this rules myself.