Author Topic: is it simple to writeline to .txt file?  (Read 1927 times)

0 Members and 1 Guest are viewing this topic.

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
is it simple to writeline to .txt file?
« on: March 06, 2013, 05:50:34 AM »
Want to export selected text to text file using C#. I've got everything done except the most important part lol... can someone please show me an example of writing to a .txt file?  Thanks!

nobody

  • Swamp Rat
  • Posts: 861
  • .net stuff
Re: is it simple to writeline to .txt file?
« Reply #1 on: March 06, 2013, 05:55:31 AM »
Nevermind...found the solution I believe. Thanks!

http://forum.codecall.net/topic/40878-c-tutorial-writing-text-files/

fixo

  • Guest
Re: is it simple to writeline to .txt file?
« Reply #2 on: March 06, 2013, 03:18:51 PM »
Found in my codes see if this working good for you
Code: [Select]
       // using System.Linq;
        // using System.IO;
       
        [CommandMethod("pointstocsv","cssp", CommandFlags.UsePickSet )]
        static public void testWritePoints()
        {
            Document doc = Autodesk.AutoCAD.ApplicationServices.Application.DocumentManager.MdiActiveDocument;
            Database db=doc.Database;
            Editor ed = doc.Editor;

            Point3dCollection pts = new Point3dCollection();
            Point3d pt=new Point3d();
            PromptPointOptions ppo = new PromptPointOptions("\nPick point (or hit Enter to Exit loop): ");
            ppo.AllowNone = true;
            PromptPointResult res;

            do
            {
                res = ed.GetPoint(ppo);
                ed.WriteMessage("\n" + res.Status.ToString());
                if (res.Status == PromptStatus.OK)
                    pt = res.Value;
                pts.Add(pt);

            } while (res.Status == PromptStatus.OK);

            string points = PointsToCsv(pts, ";", db.Dimdec);// change number of decimals here, I'm using drawing decimals variable
            WriteToTextFile(@"huh.csv", points);// change full path name here
        }

        public static string DoublesToCsv(List<List<double>> myList, string del)
        {
            StringBuilder sb = new StringBuilder();
            myList.ForEach(x => sb.Append(string.Join(del, x.Select(a => a.ToString()).ToArray()) + "\n"));
            return sb.ToString();
        }
        /// <summary>
        /// Convert Point3dCollection to csv text
        /// </summary>
        /// <param name="ptlist">points</param>
        /// <param name="sep">delimiter</param>
        /// <param name="prec">precision</param>
        /// <returns></returns>
        public static string PointsToCsv(Point3dCollection ptlist, string sep, int prec)
        {
            StringBuilder sb = new StringBuilder();
            string pc = ":f" + prec.ToString();
          foreach(Point3d pt in  ptlist)
              sb.Append(string.Format("{0" + pc + "}" + sep + "{1" + pc + "}" + sep + "{2" + pc + "}\n", pt.X, pt.Y, pt.Z));
            return sb.ToString();
        }
        public static void WriteToTextFile(string fileName, string txt)//O'Reily
        {
            if (!File.Exists(fileName))
            {
                using (StreamWriter file = File.CreateText(fileName))
                {

                    file.Write(txt);
                }
            }
            else
            {
                using (StreamWriter file = File.AppendText(fileName))
                {

                    file.Write(txt);
                }
            }
        }