Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Streamwriter output

Streamwriter output

Scheduled Pinned Locked Moved C#
17 Posts 5 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Pierre besquent

    Hi Pete, U seem u don't understand my problem well. Me i save my file in a known path of my project (this is absolutely for me) that i write to u. Then I give the user the choice to save his file whatever he likes using this code:

    StreamWriter report = (StreamWriter) GGetValue ("_Report");
    string myPath = (string) this.GGetValue ("myPath");
    this.BodyPage = "list.aspx";
    string file_name = "test.txt";
    string PresenceClientExtension = "txt";
    //if ( report)
    //{
    GRemoveValue ("_Report");
    // Néttoyer le flux en cours.
    this.Response.ClearHeaders ();
    this.Response.Clear ();
    // Re-écrire l'entête.
    this.Response.AddHeader ("content-disposition", "attachment;filename=" + myPath);
    this.Response.ContentType = "application/octet-stream";
    // Re-écrire le contenu.
    this.Response.ContentEncoding = Encoding.GetEncoding ("windows-1252");
    this.Response.Write (report.ToString());
    this.Response.Flush ();
    this.Response.End ();

    All works fine.the problem appears in the user side.when he save his file and open it he founds:"System.IO.Streamwriter" that is not the data that I have in the file on my project. this is my real problem. :) ty

    P Offline
    P Offline
    Pete OHanlon
    wrote on last edited by
    #8

    Ah, I see what you are saying. The problem occurs because you are using the ToString() method of StreamWriter when you do report.ToString();. This just writes out the name of the class. All you really need to do is write out a simple string value (that's what is being expected by Response.Write which is a stream writer in it's own right). Simply read the data you need and store it in a string (there are many suitable ways of doing this), and pass that into Response.Write instead.

    I'm not a stalker, I just know things. Oh by the way, you're out of milk.

    Forgive your enemies - it messes with their heads

    My blog | My articles | MoXAML PowerToys | Onyx

    P 1 Reply Last reply
    0
    • P Pierre besquent

      Hi, I used it. It appears to the user if he wants to : -open file -save file -cancel after choosing open or save, the user founds in his file " System.IO.streamwriter" this is my difficulty ty

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #9

      Pierre besquent wrote:

      after choosing open or save, the user founds in his file " System.IO.streamwriter"

      Sorry, but I don't understand this sentence, could you clarify?

      I must get a clever new signature for 2011.

      P P 2 Replies Last reply
      0
      • L Lost User

        Pierre besquent wrote:

        after choosing open or save, the user founds in his file " System.IO.streamwriter"

        Sorry, but I don't understand this sentence, could you clarify?

        I must get a clever new signature for 2011.

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #10

        He's saying that the file contains the name of the class, not the output he's expecting - not surprising since he's used StreamWriter.ToString().

        I'm not a stalker, I just know things. Oh by the way, you're out of milk.

        Forgive your enemies - it messes with their heads

        My blog | My articles | MoXAML PowerToys | Onyx

        1 Reply Last reply
        0
        • P Pierre besquent

          Hi every body, I wanna generate a .txt file.I use the streamwriter to write my data to a file like this:

          public static StreamWriter BuildTxtFichierVirement (ISReglementViewCollection reglements, ISISalarieCollection salaries, string myPath)
          {
          try
          {
          // Instanciation du StreamWriter avec passage du nom du fichier
          StreamWriter monStreamWriter = new StreamWriter(myPath, true, System.Text.Encoding.ASCII);
          foreach (ISReglementView reglement in reglements)
          {
          #region Date Virement ------------------------
          string strYear = reglement.DateReglement.Year.ToString ();
          string strMonth = "";
          string strDay = "";
          if (reglement.DateReglement.Month > 9)
          strMonth = reglement.DateReglement.Month.ToString ();
          else
          strMonth = "0" + reglement.DateReglement.Month.ToString ();

                      if (reglement.DateReglement.Day > 9)
                          strDay = reglement.DateReglement.Day.ToString ();
                      else
                          strDay = "0" + reglement.DateReglement.Day.ToString ();
          
                      string strDate = strDay + strMonth + (strYear.ToCharArray ()) \[strYear.Length - 1\];
          
                      string          date = reglement.DateReglement.Year.ToString ();
                      #endregion
          
          		    #region Entetes + EuroFranc  ------------------
                      string		        strEnteteHead = "0302";
                      string		        strEnteteBody = "0602";
                      string		        strEnteteSum  = "0802";
                      string		        strEuro     = "E";
          		    string		        strFranc = "F";
                      #endregion
          
          		    #region Données RIB  --------------------------
          		    string agence = "mdmff";
          					string guichet = "hhlkj";
          						string cle = "kjkj";
          							string compte = "lkjlkj" ;
          								string domicilationbancaire = "ljkjol" ;
          		   
          		    #endregion
          
          			//Ecriture du texte dans votre fichier 
          			monStreamWriter.WriteLine(strEnteteHead + Inserer\_Espaces(8) + agence + Inserer\_Espaces(7) + strDate + domicilationbancaire + reglement.ID + Inserer\_Espaces(15) + strEuro + Inserer\_Espaces(5) + guichet + compte + Inserer\_Espaces(47) + agence + Inserer\_Espaces(6)); 
          			monStreamWriter.WriteLine(strEnteteBody +Inserer\_Espaces(8) + agence + reglement.ID + reglement.Nom + reglement.Banque + Inserer\_Espaces(12) + agence + compte + reglement.Montant + agen
          
          L Offline
          L Offline
          Luc Pattyn
          wrote on last edited by
          #11

          your code could be simplified quite a bit: 1. formatting numbers with specified width: use custom number formatting, so replace

          if (reglement.DateReglement.Month > 9)
          strMonth = reglement.DateReglement.Month.ToString ();
          else
          strMonth = "0" + reglement.DateReglement.Month.ToString ();

          by

          strMonth = reglement.DateReglement.Month.ToString ("D2");

          2. getting last character of a number: use modulo 10, so replace

          string strYear = reglement.DateReglement.Year.ToString ();
          s=(strYear.ToCharArray ()) [strYear.Length - 1];

          by

          s=(reglement.DateReglement.Year%10).ToString();

          3. FYI: If you were to require two-digit year numbers, the whole lot could collapse to a single line of code:

          strDate=reglement.DateReglement.ToString("ddMMyy");

          If interested, maybe read this[^]. 4. And I assume Inserer_Espaces(N) just returns the number of spaces specified, something new string(' ', N) does too. BTW: are you familiar with string.PadLeft() and string.PadRight()? :)

          Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

          Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

          P 1 Reply Last reply
          0
          • L Lost User

            Pierre besquent wrote:

            after choosing open or save, the user founds in his file " System.IO.streamwriter"

            Sorry, but I don't understand this sentence, could you clarify?

            I must get a clever new signature for 2011.

            P Offline
            P Offline
            Pierre besquent
            wrote on last edited by
            #12

            hi, when the user choose " open" or "save" icon (in the dialog appears to him),then he opens his file,it contains just "System.IO.Streamwriter" ty

            1 Reply Last reply
            0
            • L Luc Pattyn

              your code could be simplified quite a bit: 1. formatting numbers with specified width: use custom number formatting, so replace

              if (reglement.DateReglement.Month > 9)
              strMonth = reglement.DateReglement.Month.ToString ();
              else
              strMonth = "0" + reglement.DateReglement.Month.ToString ();

              by

              strMonth = reglement.DateReglement.Month.ToString ("D2");

              2. getting last character of a number: use modulo 10, so replace

              string strYear = reglement.DateReglement.Year.ToString ();
              s=(strYear.ToCharArray ()) [strYear.Length - 1];

              by

              s=(reglement.DateReglement.Year%10).ToString();

              3. FYI: If you were to require two-digit year numbers, the whole lot could collapse to a single line of code:

              strDate=reglement.DateReglement.ToString("ddMMyy");

              If interested, maybe read this[^]. 4. And I assume Inserer_Espaces(N) just returns the number of spaces specified, something new string(' ', N) does too. BTW: are you familiar with string.PadLeft() and string.PadRight()? :)

              Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

              Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

              P Offline
              P Offline
              Pierre besquent
              wrote on last edited by
              #13

              Hi Luc, thank u a lot for the simplification of my code.I wanna know a solution for my problem :) ty

              P L 2 Replies Last reply
              0
              • P Pierre besquent

                Hi Luc, thank u a lot for the simplification of my code.I wanna know a solution for my problem :) ty

                P Offline
                P Offline
                Pete OHanlon
                wrote on last edited by
                #14

                I told you what the solution was. I just didn't write the code for you - that's up to you.

                I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                Forgive your enemies - it messes with their heads

                My blog | My articles | MoXAML PowerToys | Onyx

                1 Reply Last reply
                0
                • P Pierre besquent

                  Hi Luc, thank u a lot for the simplification of my code.I wanna know a solution for my problem :) ty

                  L Offline
                  L Offline
                  Luc Pattyn
                  wrote on last edited by
                  #15

                  you already got the solution from others. overall suggestion: use fewer ToString() calls, they often are redundant, wasteful, and sometimes wrong. :)

                  Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                  Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                  P 1 Reply Last reply
                  0
                  • P Pete OHanlon

                    Ah, I see what you are saying. The problem occurs because you are using the ToString() method of StreamWriter when you do report.ToString();. This just writes out the name of the class. All you really need to do is write out a simple string value (that's what is being expected by Response.Write which is a stream writer in it's own right). Simply read the data you need and store it in a string (there are many suitable ways of doing this), and pass that into Response.Write instead.

                    I'm not a stalker, I just know things. Oh by the way, you're out of milk.

                    Forgive your enemies - it messes with their heads

                    My blog | My articles | MoXAML PowerToys | Onyx

                    P Offline
                    P Offline
                    Pierre besquent
                    wrote on last edited by
                    #16

                    Hi, Can u please explain to me more "(that's what is being expected by Response.Write which is a stream writer in it's own right). Simply read the data you need and store it in a string (there are many suitable ways of doing this), and pass that into Response.Write instead. I don't understand well u solution. ty

                    1 Reply Last reply
                    0
                    • L Luc Pattyn

                      you already got the solution from others. overall suggestion: use fewer ToString() calls, they often are redundant, wasteful, and sometimes wrong. :)

                      Luc Pattyn [Forum Guidelines] [My Articles] Nil Volentibus Arduum

                      Please use <PRE> tags for code snippets, they preserve indentation, improve readability, and make me actually look at the code.

                      P Offline
                      P Offline
                      Pierre besquent
                      wrote on last edited by
                      #17

                      Hi, I found now the solution :laugh: . I use the streamreader like this:

                      StreamReader report = (StreamReader) GGetValue ("_Report");
                      string myPath = (string) this.GGetValue ("myPath");
                      string ligne = report.ReadLine ();
                      this.BodyPage = "list.aspx";
                      string file_name = "test.txt";
                      string PresenceClientExtension = "txt";
                      // Lecture de toutes les lignes et affichage de chacune sur la page
                      GRemoveValue ("_Report");
                      // Néttoyer le flux en cours.
                      this.Response.ClearHeaders ();
                      this.Response.Clear ();
                      // Re-écrire l'entête.
                      this.Response.AddHeader ("content-disposition", "attachment;filename=" + myPath);
                      this.Response.ContentType = "text/txt";
                      // Re-écrire le contenu.
                      this.Response.ContentEncoding = Encoding.GetEncoding ("windows-1252");
                      while (ligne != null)
                      {
                      this.Response.Write (ligne);
                      this.Response.Write("</br>");
                      this.Response.Flush ();
                      ligne = report.ReadLine();
                      }
                      this.Response.End ();
                      report.Close ();

                      All works fine now Thank u a lot for u all for u big help ;)

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups