Click here to Skip to main content
15,903,203 members
Home / Discussions / C#
   

C#

 
QuestionChange TabControl ItemSize dynamically? Pin
Member 17028208-Jun-05 1:31
Member 17028208-Jun-05 1:31 
AnswerRe: Change TabControl ItemSize dynamically? Pin
codeprojectin8-Jun-05 18:12
codeprojectin8-Jun-05 18:12 
GeneralDisplay data from text file to LISTVIEW Pin
ksanju10008-Jun-05 0:34
ksanju10008-Jun-05 0:34 
GeneralRe: Display data from text file to LISTVIEW Pin
Carsten Zeumer8-Jun-05 1:41
Carsten Zeumer8-Jun-05 1:41 
GeneralRe: Display data from text file to LISTVIEW Pin
ksanju10008-Jun-05 3:12
ksanju10008-Jun-05 3:12 
GeneralRe: Display data from text file to LISTVIEW Pin
Carsten Zeumer8-Jun-05 3:48
Carsten Zeumer8-Jun-05 3:48 
GeneralRe: Display data from text file to LISTVIEW Pin
ksanju10008-Jun-05 5:08
ksanju10008-Jun-05 5:08 
GeneralRe: Display data from text file to LISTVIEW Pin
Carsten Zeumer8-Jun-05 6:09
Carsten Zeumer8-Jun-05 6:09 
Hi Sanjeev,

first one comment to your code. You should either use a using block around the usage of the StreamReader or at least close it after use.

I hope the code below gives you some idea on how you could do it.
The performance will decrease by every entry made to "oldjob.txt" since you can only search seqentially. If you exceed several thousand lines you should move to some database driven model.

using System;
using System.IO;
using System.Collections;

namespace CodeProject
{
	/// <summary>
	/// CLass that might do somethings you need?
	/// </summary>
	public class AppendAndMove
	{
		private const int LIMIT  = 10;
		private int m_LinesWritten = 0;
		private string m_File1 = @"job.txt";
		private string m_File2 = @"oldjob.txt";

		public AppendAndMove()
		{
			// get lines in m_File1 so far...
			if (File.Exists(m_File1))
			{
				using(StreamReader sr = new StreamReader(this.m_File1) )
				{
					while (sr.ReadLine() != null)
						this.m_LinesWritten++;
				}
			}
		}

		/// <summary>
		/// appends the content of file1 to file2
		/// after that file1 is deleted
		/// </summary>
		private void MoveContent()
		{
			// move content
			using(StreamWriter sw = new StreamWriter(this.m_File2,true))
			{
				using(StreamReader sr = new StreamReader(this.m_File1) )
				{
					string line = sr.ReadLine();
					while (line != null)
					{
						sw.WriteLine(line);
						line = sr.ReadLine();
					}
				}
			}
			// delete file
			File.Delete(this.m_File1);
		}

		/// <summary>
		/// add a line to the file1 file
		/// if the file contains 10 lines, move the content
		/// to file2
		/// if m_LinesWritten is zero the file will be emptied
		/// </summary>
		/// <param name="line">the line to add</param>
		public void AddLine(string line)
		{
			using(StreamWriter sw = new StreamWriter(this.m_File1,true))
			{
				sw.WriteLine(line);
				m_LinesWritten++;
			}

			// reacht the limit?
			if (m_LinesWritten == LIMIT)
			{
				MoveContent();
				// reset count of written lines in file1
				m_LinesWritten =0;
			}
		}

		private string[] FindValueInFile(string fileName,int columnIndex, string value)
		{
			ArrayList result = new ArrayList();
			if (File.Exists(fileName))
			{
				using(StreamReader sr = new StreamReader(fileName) )
				{
					string line = sr.ReadLine();
					while (line != null)
					{
						string[] columns = line.Split(',');
						// got atleast columnIndex cols?
						if (columns.Length > columnIndex)
						{
							// is the desired value in the specified column?
							if (columns[columnIndex] == value)
							{
								// Yes, add to result...
								result.Add(line);
							}
						}
						// read next line
						line = sr.ReadLine();
					}
				}
			}
			//return result
			return (string[]) result.ToArray(typeof(string));
		}

		/// <summary>
		/// finds all lines in both files that have the specified value
		/// int the specified column
		/// </summary>
		/// <param name="columnIndex">0-based index of the column</param>
		/// <param name="value">the value to search for</param>
		/// <returns></returns>
		public string[] FindValue(int columnIndex, string value)
		{
			ArrayList result = new ArrayList();
			// search file1
			result.AddRange(FindValueInFile(this.m_File1,columnIndex, value));
			// search file2
			result.AddRange(FindValueInFile(this.m_File2,columnIndex, value));

			//return result
			return (string[]) result.ToArray(typeof(string));
		}


		/// <summary>
		/// Test the class
		/// </summary>
		[STAThread]
		static void Main() 
		{
			// Application.Run(new Form1());
			AppendAndMove testClass = new AppendAndMove();
			System.Text.StringBuilder sb = new System.Text.StringBuilder();
			Random rnd = new Random();
			for(int i=0;i<100;i++)
			{
				sb.Length=0;
				for(int t = 0;t<8;t++)
				{
					sb.Append(rnd.Next(1,100));
					if (t != 7)
						sb.Append(",");
				}
				testClass.AddLine(sb.ToString());
			}

			string[] foundLines = testClass.FindValue(2,"50");
			foreach(string s in foundLines)
				System.Console.Out.WriteLine(s);

		}
	}
}


/cadi

24 hours is not enough
GeneralRe: Display data from text file to LISTVIEW Pin
ksanju10009-Jun-05 1:12
ksanju10009-Jun-05 1:12 
GeneralRe: Display data from text file to LISTVIEW Pin
ksanju10009-Jun-05 19:40
ksanju10009-Jun-05 19:40 
Generalresize the Button control Pin
prinz_simo8-Jun-05 0:26
prinz_simo8-Jun-05 0:26 
GeneralRe: resize the Button control Pin
MoustafaS8-Jun-05 2:21
MoustafaS8-Jun-05 2:21 
GeneralMS Word spell checker Pin
Gktony8-Jun-05 0:01
Gktony8-Jun-05 0:01 
GeneralRe: MS Word spell checker Pin
Anonymous9-Jun-05 4:03
Anonymous9-Jun-05 4:03 
Generalload a 256 color cursor Pin
Anonymous7-Jun-05 23:40
Anonymous7-Jun-05 23:40 
GeneralText box not accepting focus Pin
GazzaJ7-Jun-05 23:14
GazzaJ7-Jun-05 23:14 
GeneralRe: Text box not accepting focus Pin
NormDroid8-Jun-05 0:10
professionalNormDroid8-Jun-05 0:10 
GeneralRe: Text box not accepting focus Pin
GazzaJ8-Jun-05 1:52
GazzaJ8-Jun-05 1:52 
GeneralLog in/Log out time from Active Directory Pin
Zapss7-Jun-05 22:38
Zapss7-Jun-05 22:38 
GeneralRe: Log in/Log out time from Active Directory Pin
Dave Kreskowiak8-Jun-05 6:56
mveDave Kreskowiak8-Jun-05 6:56 
GeneralRe: Log in/Log out time from Active Directory Pin
Zapss10-Jun-05 18:58
Zapss10-Jun-05 18:58 
GeneralCreating a self-executing file from within a C# application Pin
Anonymous7-Jun-05 22:34
Anonymous7-Jun-05 22:34 
GeneralRe: Creating a self-executing file from within a C# application Pin
Anonymous8-Jun-05 1:50
Anonymous8-Jun-05 1:50 
Questionuser's password from active directory ? Pin
Tee+7-Jun-05 22:31
Tee+7-Jun-05 22:31 
AnswerRe: user's password from active directory ? Pin
Carsten Zeumer7-Jun-05 22:36
Carsten Zeumer7-Jun-05 22:36 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.