Hi .... i got one problem....

i am doing a project. I use StreamReader to read my content in one file.
i need to change tht file into the string type. Then, i need to change the string type to string array....who know how to do this one....

Hi .... i got one problem....

i am doing a project. I use StreamReader to read my content in one file.
i need to change tht file into the string type. Then, i need to change the string type to string array....who know how to do this one....

i need to do in C# window form application.
thanks ..

Look at this example:

private void GettingFileContent()
        {
            string path = @"C:\1\test3.txt";
                  
            //example of two different arrays:
            //for string array (string[]) you need to define the lenght of it
            //for the generic list you dont need to

            //1.
            int rows = File.ReadAllLines(path).Length;
            string[] strArray = new string[rows];
            //2.
            List<string> listArray = new List<string>();

            using (StreamReader sr = new StreamReader(path))
            {
                string line;
                int count = 0;
                while ((line = sr.ReadLine()) != null)
                {
                    strArray[count++] = line;
                    listArray.Add(line);
                }
            }
        }

This coding is I use for open and print my content of one file..so how i change the file type to string and change from string to string array????

private void button2_Click(object sender, EventArgs e)
        {
            using ( OpenFileDialog fileOpen = new OpenFileDialog())
            {
                try
                {
                    fileOpen.Filter = "All files (*.*)|*.*";
                    fileOpen.InitialDirectory = ".\\";
                    fileOpen.Title = "Open";

                    if (fileOpen.ShowDialog() == DialogResult.OK)
                    {
                        textBox1.Text = fileOpen.FileName;
                    }

                    StreamReader sr = new StreamReader(fileOpen.FileName);

                    string str = sr.ReadToEnd();
                    textBox7.Text = str;
                    sr.Close();
              }

                catch (Exception errorMsg)
                {
                    MessageBox.Show(errorMsg.Message);
                }
            }

        }

What do you mean with "change the file type to string"?
if you want to convert to string you only do on the end of some value "someValue.ToString()".

To array?
What exactly would you like to get into array?

erm...how to explain..?

OK....Now, I have many group of numbers(eg. 123 234 345 456 234 123....).I store them in one text file. When I open the text file,the group of numbers are arranged in horizontal.I want change those group of numbers to string array so that I can split them to one group of numbers and arrange in vertical.
Example:
123
234
345
456
234
123
......

so I need to change the file content to string type. After that, I need to change them to string array and use split method to do the splitting .So that, the group of numbers will change to vertical like i mention above.
but I tried many time but still cannot get it.
so you can help me...?

Do you have only one line of numbers in the text file?
And why do you need them in "vertical" position. You know, horizontal and vertical for computer means nothing. This is only you who imagine them to be arranged this way.

This example converts and puts a string into an array (like horizontal way).
Did you mean something like that:

string path = @"C:\1\test6.txt";
            using (StreamReader sr = new StreamReader(path))
            {
                string allText = null;
                string line;
                while ((line = sr.ReadLine()) != null)
                    allText += line;

                string[] array = allText.Split(' ');
            }

This example converts and puts a string into an array (like horizontal way).
Did you mean something like that:

string path = @"C:\1\test6.txt";
            using (StreamReader sr = new StreamReader(path))
            {
                string allText = null;
                string line;
                while ((line = sr.ReadLine()) != null)
                    allText += line;

                string[] array = allText.Split(' ');
            }

oo..
I got many lines of numbers. (in horizontal)
one line could have many groups of numbers.
I need to change to the vertical lines..
BTW, thanks for helping me.

If I understand you connrectl, you now has grouped number in hortizontal way like:
(reading line by line in horizontal way)
12 73 53
45 14 95
14 65 37

... now you would like to group these number in vertical way, so the group will be:
12 45 14
73 14 65
53 95 37

(the number are now in horizontal way, but taken vertically from the upper example.
Am I correct?

If I understand you connrectl, you now has grouped number in hortizontal way like:
(reading line by line in horizontal way)
12 73 53
45 14 95
14 65 37

... now you would like to group these number in vertical way, so the group will be:
12 45 14
73 14 65
53 95 37

(the number are now in horizontal way, but taken vertically from the upper example.
Am I correct?

yaya....because after this, i need to do the sortedlist....

Wait, Iam trying to do ...

Yo, I`m did the code for you. I hope you like it. I put some effort into it today (yest. didnt have time, sorry).

To test it, create a win form, and put two labels on it. But dont exagurate with the numbers (just for a text, use maybe a few ten).
This is the code:

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Data.SqlClient;

namespace Jan19NumberArray
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
            label1.Text = "";
            label2.Text = "";
            YourMain();
        }

        private void YourMain()
        {
            string[] lines = ReadingFile();
            List<List<int>> list = SeperatingNumbers(lines);
            int[,] array = Create2DIntArray(list);
            ShowingArray("horizontal", array);
            int[,] newArray = Transform(array);
            ShowingArray("vertical", newArray);
        }

        private string[] ReadingFile()
        {
            string path = @"C:\1\test6.txt";
            string[] lines = File.ReadAllLines(path);
            return lines;
        }

        private List<List<int>> SeperatingNumbers(string[] lines)
        {

            List<List<int>> listOuter = new List<List<int>>();
            List<int> listInner;
            for (int i = 0; i < lines.Length; i++)
            {
                listInner = new List<int>();
                string[] rowNumbers = lines[i].Split(' ');
                for (int j = 0; j < rowNumbers.Length; j++)
                {
                    int num = Convert.ToInt32(rowNumbers[j]);
                    listInner.Add(num);
                }
                listOuter.Add(listInner);
            }
            return listOuter;
        }

        private int[,] Create2DIntArray(List<List<int>> listOuter)
        {
            int rows = listOuter.Count;
            int maxColumns = listOuter.Max(a => a.Count);
            int[,] array = new int[rows, maxColumns];

            for (int k = 0; k < listOuter.Count; k++)
            {
                List<int> In_list = listOuter[k];
                for (int l = 0; l < listOuter[k].Count; l++)
                {
                    array[k, l] = In_list[l];
                }
            }
            return array;
        }
        
        T[,] Transform<T>(T[,] array)
        {
            int rows = array.GetLength(0);
            int cols = array.GetLength(1);

            // Make the result array
            T[,] result = new T[cols, rows];

            for (int i = 0; i < rows; ++i)
                for (int j = 0; j < cols; ++j)
                    result[j, i] = array[i, j];

            return result;
        }

        //ADDITIONAL:
        private void ShowingArray(string type, int[,] array)
        {
            int rows = array.GetLength(0);
            int cols = array.GetLength(1);

            for (int i = 0; i < rows; i++)
            {
                for (int j = 0; j < cols; j++)
                {
                    if (type == "horizontal")
                    {
                        if (array[i, j] > 0)
                            label1.Text += array[i, j].ToString() + " ";
                    }
                    else
                    {
                        if (array[i, j] > 0)
                            label2.Text += array[i, j].ToString() + " ";
                    }
                }
                if (type == "horizontal")
                    label1.Text += System.Environment.NewLine;
                else
                    label2.Text += System.Environment.NewLine;
            }
        }
    }
}

I hope this is it, and that I understood your requirments.
If not, let me know...
cya,
Mitja

commented: ++++++++++++ +1

if you want to load text from file use this -

string YourText = System.IO.File.ReadAllText(Application.StartupPath + @"/yourFileName.txt", System.Text.Encoding.UTF8);
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.