Tuesday, September 7, 2010

working with File in c#

//To replace and edit a text file
StringBuilder newFile = new StringBuilder();




string temp = "";



string[] file = File.ReadAllLines(@"C:\Documents and Settings\john.grove\Desktop\1.txt");



foreach (string line in file)



{



if (line.Contains("string"))



{



temp = line.Replace("string", "String");



newFile.Append(temp + "\r\n");



continue;



}



newFile.Append(line + "\r\n");



}

File.WriteAllText(@"C:\Documents and Settings\john.grove\Desktop\1.txt", newFile.ToString());




//if file doesn't exist, search for the path


if (!System.IO.File.Exists(testContainer.Name))

{

System.IO.FileInfo fInfo = new System.IO.FileInfo(testContainer.Name);

string dllName = fInfo.Name;



foreach (string path in DLLPaths)

{

//if dll existed in this path, update the qtm container with the new path

if (System.IO.File.Exists(System.IO.Path.Combine(path, dllName)))

{

testContainer.Name = System.IO.Path.Combine(path, dllName);

//stop searching

break;

}

}

}

No comments:

Post a Comment