class FileWork { public static Stream BrowseFile() { OpenFileDialog f = new OpenFileDialog(); Stream fs = null; f.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop); f.Filter = "txt files (*.txt)|*.txt"; if(f.ShowDialog() == DialogResult.OK) fs = f.OpenFile(); return fs; } public static string ReadFile(Stream fileStream) { fileStream.Seek(0, SeekOrigin.Begin); StreamReader reader = new StreamReader(fileStream); return reader.ReadToEnd(); } public static int[,] SaveMatrix(Stream fileStream, int N, int M) { string file = ReadFile(fileStream); string[] fileArr; file = file.Remove(0, 5); fileArr = file.Split(new Char[] {'\n', '\r'}, StringSplitOptions.RemoveEmptyEntries); int len = N * M; int[,] matrix = new int[len, len]; for (int i = 0; i < len; i++) for (int j = 0; j < len; j++) matrix[i, j] = (int)Char.GetNumericValue(fileArr[i][j]); return matrix; } public static void GetSize(Stream fileStream, out int N, out int M) { string file = ReadFile(fileStream); string firstNum = ""; string secondNum = ""; int i = 0; while (!file[i].Equals('\r')) { while (!file[i].Equals(' ')) { firstNum += file[i]; i++; } i++; while (!file[i].Equals('\r')) { secondNum += file[i]; i++; } break; } N = int.Parse(firstNum); M = int.Parse(secondNum); } }