Posted By: BenZilla | Jan 10th, 2006 @ 11:32 PM
page 1 of 1
Comments: 23 | Views: 14024
My noobness has caught up with me Wink

Just trying to read a txt file stream and then put it in a multiline textbox. Here's some sample code Big Smile

Stream myStream;
//open filedialog stuff.
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    string sPythonString = Convert.ToString(myStream);

                    txtInputScript.Text = sPythonString;
                }

So i'm thinking, great Wink But when I open up the text file this is all I get is this in the textbox.

System.IO.FileStream
Yggdrasil
Yggdrasil
Pour me a cab, 'cause I can't drink no more.
BenZilla wrote:
My noobness has caught up with me Wink

Just trying to read a txt file stream and then put it in a multiline textbox. Here's some sample code Big Smile

Stream myStream;
//open filedialog stuff.
openFileDialog1.Filter = "txt files (*.txt)|*.txt|All files (*.*)|*.*";

if ((myStream = openFileDialog1.OpenFile()) != null)
                {
                    string sPythonString = Convert.ToString(myStream);

                    txtInputScript.Text = sPythonString;
                }

So i'm thinking, great Wink But when I open up the text file this is all I get is this in the textbox.

System.IO.FileStream


A stream is not a string - it's a wrapper object around a stream of data, and as such you can't just cast it to a string. Convert.ToString() is the equivalent of calling myStream.ToString(), which (for most objects) simply returns the type name for lack of anything better.

What you need to do is read the text from the stream. Simplest way (in Framework v1.1) is this:
<BR>using (StreamReader reader = new StreamReader(myStream))<BR>{<BR>   string sPythonString = reader.ReadToEnd();<BR>   txtInputScript.Text = sPythonString;<BR>}

To make things even simpler, you can simply pass the path to the file directly to the StreamReader constructor and have it implicitly create a FileStream.

In .NET 2.0 it's even simpler - ridiculously so, in fact:
<BR>string sPythonString = System.IO.File.ReadAllText(fileName);<BR>
Yggdrasil
Yggdrasil
Pour me a cab, 'cause I can't drink no more.
BenZilla wrote:
What are C#'s equivalent to <vector>? So I can groooooow my arrays.


In .NET 1.1 - use an ArrayList. In .NET 2.0 you can use a strongly-typed generics - List<int>.

I'm not sure about the List<>, but an ArrayList can be easily converted to a standard array once you've finished adding to it. Always best to expose strongly typed arrays with public interfaces. Exposing an arraylist is icky.

BenZilla wrote:

And also, let's say I have a command line application(non .net) how would I catch out puts from it?


Use the System.Diagnostics.ProcessStartInfo class to define the process properties, including redirecting the standard output to a stream. The you use System.Diagnostics.Process.Start() to launch it, and access the stream to get the process' output.


Yggdrasil
Yggdrasil
Pour me a cab, 'cause I can't drink no more.
BenZilla wrote:
Great info, it's good because I don't really know what any of the class names are, so when you give me a hint I can either search MSDN or just "feel" my way through it

Thank's.



Glad to help.
Thousands of classes and interfaces scattered around the framework. Once you have the names you can guess your way around with Intellisense, and use MSDN to validate your guesses. Best way to learn.
Maurits
Maurits
AKA Matthew van Eerde
How about

new ProcessStartInfo("bspzip.exe", "-dir " + sMapLocation );
Maurits
Maurits
AKA Matthew van Eerde
DataGridViewRow article

For Each SelectedRow As DataGridViewRow In _
DataGridView1.SelectedRows
MessageBox.Show( _
SelectedRow.Cells("CustomerID").Value)
Next
You probably want something like row.Cells("something").Value
JohnAskew
JohnAskew
9 girl in pink sweater
Yggdrasil wrote:


In .NET 2.0 it's even simpler - ridiculously so, in fact:


string sPythonString = System.IO.File.ReadAllText(fileName);

 



That's awesome.
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
BenZilla wrote:
Great stuff, I did it like this(I had .value before but it was just throwing errors)

string cellValue = dtgFileToAdd.Rows[rowNumber].Cells[cellNumber].Value.ToString();

Ok, heres a new questions How do I force a new line? As you cans ee I have this piece of code.

                System.IO.File.WriteAllText("temp.txt", cellValue);

Just put's all the text on the first line.

                System.IO.File.WriteAllText("temp.txt", cellValue + "\n");

Does not work



Try this:
System.IO.File.WriteAllText("temp.txt", cellValue + Environment.NewLine);
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
If you're writing more than one line to a file, don't use WriteAllText, since this opens and closes the file every time which is a performance overhead, and indeed overwrites the file, it does not append to it.

You could first create the entire file contents as a string and then write that to a file using WriteAllText, but it's better to use the StreamWriter class instead:
using( System.IO.StreamWriter writer = new System.IO.StreamWriter("temp1.txt") )<BR>{<BR>  foreach (DataGridViewRow row in rows)<BR>  {<BR>     if (row.IsNewRow) continue;<BR><BR>     string cellValue = dtgFileToAdd.Rows[rowNumber].Cells[cellNumber].Value.ToString();<BR>     row.HeaderCell.Value = "Row " + rowNumber;<BR>     writer.WriteLine(cellValue);<BR><BR>     rowNumber = rowNumber + 1;<BR>  }<BR>}
Sven Groot
Sven Groot
My name has 9 letters. Coincidence? I think not...
Without seeing the contents of cellValue it's hard to say.
Maurits
Maurits
AKA Matthew van Eerde
Something's missing here.  Are you copying and pasting the code exactly as you have it?  Did you try stepping through it and inspecting the value of cellValue prior to the EndsWith call?
page 1 of 1
Comments: 23 | Views: 14024
Microsoft Communities