Writing strings to file

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • John

    #1

    Writing strings to file

    Hi

    Is there any easy way to write strings into a file for debugging purposes?

    Thanks

    Regards


  • Harry

    #2
    Re: Writing strings to file


    "John" <info@nospam.in fovis.co.ukwrot e in message
    news:%23M8LRgrP JHA.3932@TK2MSF TNGP02.phx.gbl. ..
    Hi
    >
    Is there any easy way to write strings into a file for debugging purposes?
    >
    Thanks
    >
    Regards
    >
    You will need a reference to System.IO

    Using sw As New StreamWriter(My FileName, True) 'last parameter tells sw to
    append to file

    sw.WrileLine(My Text)
    End Using


    Comment

    • =?Utf-8?B?ZG90TmV0RGF2ZQ==?=

      #3
      RE: Writing strings to file

      I suggest looking into Tracing. It's very powerful and allows you to log to
      files, xml, event log and more. I wrote code that makes it even more powerful
      that will appear in the next update of my open source assembly located at


      David

      =============== =============== ========
      David McCarter [Microsoft MVP]
      Improving Code Quality... One Developer at a Time

      David McCarter''''s .NET Coding Standards available at:
      http://www.cafepress.com/geekmusicart.1654787045


      "John" wrote:
      Hi
      >
      Is there any easy way to write strings into a file for debugging purposes?
      >
      Thanks
      >
      Regards
      >
      >
      >

      Comment

      • sloan

        #4
        Re: Writing strings to file


        Here is some C# code.

        This can write to your computers temp directory and "pop it" in notepad.


        But I would also look at a complete logging solution.



        private void WriteAndOrPopDe bugMessage(stri ng msg)
        {
        bool popTextFile = false;

        bool generateTempFil e = false;

        //set those values to config file settings
        popTextFile =true;//better to read from config file
        generateTempFil e =true;//better to read from config file

        if (popTextFile || generateTempFil e )
        {
        string fileName = WriteToTempFile (string.Empty, msg);
        if (popTextFile)
        {
        System.Diagnost ics.Process.Sta rt(fileName);
        }
        else
        {
        Console.WriteLi ne(fileName);
        }
        }
        }

        public string WriteToTempFile (string prefix, string toWrite)
        {

        string fileName = System.IO.Path. GetTempFileName ();
        fileName = fileName.Replac e(".tmp", ".txt");
        return WriteAFile(file Name, prefix, toWrite);

        }


        public string WriteAFile(stri ng fileName , string prefix, string
        toWrite)
        {

        System.IO.Strea mWriter writer = null;
        System.IO.FileS tream fs = null;

        try
        {

        // Writes text to a temporary file and returns path

        fs = new System.IO.FileS tream(fileName,
        System.IO.FileM ode.Append, System.IO.FileA ccess.Write);
        // Opens stream and begins writing
        writer = new System.IO.Strea mWriter(fs);
        writer.BaseStre am.Seek(0, System.IO.SeekO rigin.End);
        writer.WriteLin e(toWrite);
        writer.Flush();

        return fileName;

        }
        finally
        {
        if (null != writer)
        {
        writer.Close();
        }
        if (null != fs)
        {
        fs.Close();
        }
        }
        }






        "John" <info@nospam.in fovis.co.ukwrot e in message
        news:%23M8LRgrP JHA.3932@TK2MSF TNGP02.phx.gbl. ..
        Hi
        >
        Is there any easy way to write strings into a file for debugging purposes?
        >
        Thanks
        >
        Regards
        >

        Comment

        Working...