Streamwriter output
-
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
-
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
Ah, I see what you are saying. The problem occurs because you are using the
ToString()
method of StreamWriter when you doreport.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 byResponse.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 intoResponse.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
-
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
-
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.
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
-
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
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, somethingnew string(' ', N)
does too. BTW: are you familiar withstring.PadLeft()
andstring.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.
-
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.
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
-
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, somethingnew string(' ', N)
does too. BTW: are you familiar withstring.PadLeft()
andstring.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.
Hi Luc, thank u a lot for the simplification of my code.I wanna know a solution for my problem :) ty
-
Hi Luc, thank u a lot for the simplification of my code.I wanna know a solution for my problem :) ty
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
-
Hi Luc, thank u a lot for the simplification of my code.I wanna know a solution for my problem :) ty
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.
-
Ah, I see what you are saying. The problem occurs because you are using the
ToString()
method of StreamWriter when you doreport.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 byResponse.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 intoResponse.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
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
-
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.
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 ;)