Click here to Skip to main content
15,900,461 members
Home / Discussions / C#
   

C#

 
QuestionDataSet Fill Method via WCF?? Pin
Illegal Operation27-Jan-09 12:35
Illegal Operation27-Jan-09 12:35 
AnswerRe: DataSet Fill Method via WCF?? Pin
tasumisra28-Jan-09 1:27
tasumisra28-Jan-09 1:27 
Questioncan't open a vs 2003.net project.... Pin
AndieDu27-Jan-09 12:11
AndieDu27-Jan-09 12:11 
AnswerRe: can't open a vs 2003.net project.... Pin
Colin Angus Mackay27-Jan-09 13:37
Colin Angus Mackay27-Jan-09 13:37 
GeneralRe: can't open a vs 2003.net project.... Pin
AndieDu27-Jan-09 14:26
AndieDu27-Jan-09 14:26 
GeneralRe: can't open a vs 2003.net project.... Pin
_Maxxx_27-Jan-09 15:48
professional_Maxxx_27-Jan-09 15:48 
AnswerRe: can't open a vs 2003.net project.... Pin
AndieDu28-Jan-09 17:49
AndieDu28-Jan-09 17:49 
QuestionSearching files for String data - performance suggestions? Pin
Ryan Neil Shaw27-Jan-09 12:06
Ryan Neil Shaw27-Jan-09 12:06 
Hello fellow developers,

For simplicity sake, I know you are all busy, I'll get to the meat of the question:

What would be the fastest way to search a .DLL, .EXE and .config for a String value?

Here is the background info:
I have completed an application that searches "Locations" for configuration settings. It works very well, my only complaint is the File Searching functionality appears to be quite slow. Normally for a localized search I use the WinGrep application www.wingrep.com, which works very well and amazingly fast ( its written in C, so it should be Cool | :cool: ). I basically want the same functionality that WinGrep provides but add a nice manager feel to it, hence the application I just finished. The application was built to help us unify our broad code base of 100s of applications writen by various developers over time. Specifically to find and replace connection strings and hard coded server references. For .configs, its easy, File.ReadAllText() and use RegEx, done... for Binaries ( DLL / EXE primarily ) it was a little more difficult. I need to search binaries because some applications we do not control the source for and would have to request a change from that company division.

For small files I did the following:
<br />
Byte[] fileData = File.ReadAllBytes(file.FullName);       <br />
String fileAsString = System.Text.Encoding.Unicode.GetString(fileData);<br />

For larger files I did the below, taken from a suggestion on this site:
String line = null;
int lineNum = 0;

// open the file in binary mode
using (Stream stream = file.Open(FileMode.Open, FileAccess.Read))
{
	byte[] readBuffer = new byte[16384]; // our input buffer
	int bytesRead = 0; // number of bytes read
	int offset = 0; // offset inside read-buffer
	long filePos = 0; // position inside the file before read operation
	while ((bytesRead = stream.Read(readBuffer, offset,
	readBuffer.Length - offset)) > 0)
	{
		line = System.Text.Encoding.Unicode.GetString(readBuffer);
		
		MatchCollection matches = regEx.Matches(line);
		if (matches.Count > 0)
		{
			foreach (Match match in matches)
			{
				if (match.Success)
				{
					 StoreMatch(match.Captures[0].Value);
				}
			}
		}

		// store file position before next read
		filePos = stream.Position;

		// store the last few characters to ensure matches on "chunk boundaries"
		//offset = bufferToLookFor.Length;
		
		offset = (task.Search.Replacement.Length * 2); // crude set of a chunk boundary
		for (int i = 0; i < offset; i++)
			readBuffer[i] = readBuffer[readBuffer.Length - offset + i];
	}
}


Here are the methods used to iterate thru a "Task" for a search location
private static TaskSearchResult SearchUNC(TaskSearch task)
{
	if (!task.Location.Equals(_location))
	{
		_location = task.Location;
		_locationFilePath = new DirectoryInfo(task.Location.Path);
	}
	task.Search = new SearchRegEx(task.Search);
	
	TaskSearchResult taskResult = new TaskSearchResult();
	taskResult.Result = true;

	//Check if Directory Exists
	if (_locationFilePath.Exists)
	{
		//Call Location search, combine results
		taskResult.Combine(SearchFileLocation(_locationFilePath, task));
	}
	else
	{
		taskResult.Result = false;
		taskResult.SearchResults.Add(new SearchResult(task.Location.Path, String.Format("Directory {0} does not exist", task.Location.Path)));
	}
	
	return taskResult;
}
private static TaskSearchResult SearchFileLocation(DirectoryInfo directory, TaskSearch task)
{
	TaskSearchResult taskResult = new TaskSearchResult();
	Console.WriteLine("* File Location being searched {0}", directory.FullName);
	try
	{
		FileInfo[] files = directory.GetFiles(task.Location.Filter, SearchOption.TopDirectoryOnly);
		//Iterate thru all the files in this location
		foreach (FileInfo fi in files)
		{
			try
			{
				if (IsTextFile(fi))
				{
					taskResult.Combine(SearchFile(fi, task));
				}
				else
				{
					//TODO Possibly make the "Large File Size" a config option
					if (fi.Length > (1024 * 1024)) // 1024B * 1024 = 1MB
						taskResult.Combine(SearchFileBinaryLarge(fi, task));
					else
						taskResult.Combine(SearchFileBinary(fi, task));
				}
			}
			catch (Exception ex)
			{
				Console.WriteLine(String.Format("File :{0} Error: {1} \n", fi.FullName, ex.Message));
				taskResult.SearchResults.Add(new SearchResult(task.Location.Path, String.Format("{0} - Error:{1}", fi.FullName, ex.Message)));
				//result += String.Format("File :{0} Error: {1} \n", fi.FullName, ex.Message);
			}
		}

		//If task Location IsRecusive, get the Sub Dirs to iterate thru
		if (task.Location.IsRecursive)
		{
			//get directories to search
			DirectoryInfo[] directories = directory.GetDirectories();
			foreach (DirectoryInfo subDirectory in directories)
			{
				taskResult.Combine(SearchFileLocation(subDirectory, task));
			}
		}
	}
	catch (Exception ex)
	{
		taskResult.SearchResults.Add(new SearchResult(task.Location.Path,String.Format("{0} - Error:{1}", directory.FullName, ex.Message)));
	}

	return taskResult;
}


The IsTextFile(File file) method simply does a check in the registry for the extention and it checks for known Text Type files, its good enough for the scope of this application although I would love some feed back on that as well.

In conclusion, I would love any feedback you can provide, let me know if you need to see more code.

Kind Regards,
Ryan
AnswerRe: Searching files for String data - performance suggestions? Pin
Ryan Neil Shaw2-Feb-09 10:03
Ryan Neil Shaw2-Feb-09 10:03 
AnswerRe: Searching files for String data - performance suggestions? Pin
Ryan Neil Shaw2-Feb-09 12:40
Ryan Neil Shaw2-Feb-09 12:40 
QuestionDisplay contents of a multiline textbox for printing Pin
krup7527-Jan-09 11:46
krup7527-Jan-09 11:46 
AnswerRe: Display contents of a multiline textbox for printing Pin
led mike27-Jan-09 11:54
led mike27-Jan-09 11:54 
Questionsuppress crystal report parameter dialog box Pin
Jassim Rahma27-Jan-09 11:39
Jassim Rahma27-Jan-09 11:39 
AnswerRe: suppress crystal report parameter dialog box Pin
Wendelius27-Jan-09 11:43
mentorWendelius27-Jan-09 11:43 
GeneralRe: suppress crystal report parameter dialog box Pin
Jassim Rahma27-Jan-09 11:45
Jassim Rahma27-Jan-09 11:45 
GeneralRe: suppress crystal report parameter dialog box Pin
Wendelius27-Jan-09 11:56
mentorWendelius27-Jan-09 11:56 
QuestionDisplaying & editing a two dimensional array in DataGridView Pin
Henrik Schmiediche27-Jan-09 10:44
Henrik Schmiediche27-Jan-09 10:44 
AnswerRe: Displaying & editing a two dimensional array in DataGridView Pin
MadArtSoft28-Jan-09 0:01
MadArtSoft28-Jan-09 0:01 
GeneralRe: Displaying & editing a two dimensional array in DataGridView Pin
Henrik Schmiediche28-Jan-09 9:34
Henrik Schmiediche28-Jan-09 9:34 
QuestionListview Sub item Edit Pin
Udayaraju27-Jan-09 8:51
Udayaraju27-Jan-09 8:51 
AnswerRe: Listview Sub item Edit Pin
Wendelius27-Jan-09 8:56
mentorWendelius27-Jan-09 8:56 
AnswerRe: Listview Sub item Edit Pin
Dave Kreskowiak27-Jan-09 10:04
mveDave Kreskowiak27-Jan-09 10:04 
GeneralRe: Listview Sub item Edit Pin
Udayaraju28-Jan-09 4:10
Udayaraju28-Jan-09 4:10 
QuestionSpeeding up code Pin
Xmen Real 27-Jan-09 8:23
professional Xmen Real 27-Jan-09 8:23 
AnswerRe: Speeding up code Pin
vaghelabhavesh27-Jan-09 8:31
vaghelabhavesh27-Jan-09 8:31 

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.