Question
-
hi all,
i have single table and i want to save it in file
EX:1 a b c d2 q r t j3 w j s r
thanks for help
Answers
-
I am assuming its a single dimension string array.
Use the following
string[] table = { "1 a b c d", "2 q r t j", "3 w j s r" }; System.IO.File.WriteAllLines("c:\\test.txt", table);
If its in a 2d array, then use the following code
string[,] dataTable = new string[3,4]; ///Fill the table List<string> linesToWrite = new List<string>(); for(int rowIndex = 0; rowIndex < 3; rowIndex++) { StringBuilder line = new StringBuilder(); for(int colIndex = 0; colIndex < 4; colIndex++) line.Append(dataTable[rowIndex, colIndex]).Append(" "); linesToWrite.Add(line.ToString()); } System.IO.File.WriteAllLines("C:\\test.txt", linesToWrite.ToArray());
Thanks,
A.m.a.L
Dot Net GoodiesDon't hate the hacker, hate the code - Edited by A.m.a.L HashimMVP Wednesday, March 17, 2010 4:42 PM more code
- Marked as answer by Liliane Teng Wednesday, March 24, 2010 10:24 AM
-
I have put array in hashtable by make value (array) Myhashtable.Add("MyKay", new string[0]);
- Marked as answer by Ahmed_Soft3 Wednesday, March 24, 2010 12:38 PM
-
Hello Ahmed,
Welcome to MSDN Form.
You can use StreamReader class to realize.Here is an simple example.I hope this could help you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CA1
{
class Program
{
static void Main(string[] args)
{
string[] table = { "1 a b c d","2 q r t j","3 w j s r" };
System.IO.File.WriteAllLines("c:\\test.txt", table);
StreamReader reader = new StreamReader("c:\\test.txt", System.Text.Encoding.Default);
string[] values = reader.ReadToEnd().Split('\n');
for (int i = 0; i <3; i++)
{
System .Console.WriteLine(values[i]);
}
}
}
}
Best regards!
Liliane Teng
Please mark the replies as answers if they help and unmark them if they provide no help. Thanks- Marked as answer by Liliane Teng Wednesday, March 24, 2010 3:33 PM
All replies
-
I am assuming its a single dimension string array.
Use the following
string[] table = { "1 a b c d", "2 q r t j", "3 w j s r" }; System.IO.File.WriteAllLines("c:\\test.txt", table);
If its in a 2d array, then use the following code
string[,] dataTable = new string[3,4]; ///Fill the table List<string> linesToWrite = new List<string>(); for(int rowIndex = 0; rowIndex < 3; rowIndex++) { StringBuilder line = new StringBuilder(); for(int colIndex = 0; colIndex < 4; colIndex++) line.Append(dataTable[rowIndex, colIndex]).Append(" "); linesToWrite.Add(line.ToString()); } System.IO.File.WriteAllLines("C:\\test.txt", linesToWrite.ToArray());
Thanks,
A.m.a.L
Dot Net GoodiesDon't hate the hacker, hate the code - Edited by A.m.a.L HashimMVP Wednesday, March 17, 2010 4:42 PM more code
- Marked as answer by Liliane Teng Wednesday, March 24, 2010 10:24 AM
-
thanks A.m.a.L - aditi.com - Think Product
but how i can call data to array after saving in file
can we make somthing like hashtable but hashtable contain one value (Key - Value) and i have more than value -
Seems you are looking for one key with many values.
In codeplex one library is available which can help you
http://www.codeplex.com/PowerCollections
Thanks,
A.m.a.L
Dot Net GoodiesDon't hate the hacker, hate the code -
I have put array in hashtable by make value (array) Myhashtable.Add("MyKay", new string[0]);
- Marked as answer by Ahmed_Soft3 Wednesday, March 24, 2010 12:38 PM
-
Hello Ahmed,
Welcome to MSDN Form.
You can use StreamReader class to realize.Here is an simple example.I hope this could help you.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace CA1
{
class Program
{
static void Main(string[] args)
{
string[] table = { "1 a b c d","2 q r t j","3 w j s r" };
System.IO.File.WriteAllLines("c:\\test.txt", table);
StreamReader reader = new StreamReader("c:\\test.txt", System.Text.Encoding.Default);
string[] values = reader.ReadToEnd().Split('\n');
for (int i = 0; i <3; i++)
{
System .Console.WriteLine(values[i]);
}
}
}
}
Best regards!
Liliane Teng
Please mark the replies as answers if they help and unmark them if they provide no help. Thanks- Marked as answer by Liliane Teng Wednesday, March 24, 2010 3:33 PM