Answered by:
Cannot access the file because it being used by another process..

Question
-
Hey!
Im trying to make an search and replace in a SQL document but it just says that "Cannot access the file because it being used by another process..".. Do someone knows what im doing wrong?
string KundLänk = StandardSökvägTxt.Text.Substring(0, StandardSökvägTxt.Text.LastIndexOf(@"\") + 1); string KundMapp = KundLänk + KundNamnTxt.Text; Directory.CreateDirectory(KundMapp); //string fileToCopy = StandardSökvägTxt.Text + @"\*.sql"; //File.Copy(fileToCopy, KundMapp + Path.GetFileName(fileToCopy)); //string picturesFile = @"D:\pictures"; //string destFile = @"C:\Temp\tempFolder\"; foreach (string newPath in Directory.GetFiles(StandardSökvägTxt.Text, "*.*", SearchOption.AllDirectories)) { File.Copy(newPath, newPath.Replace(StandardSökvägTxt.Text, KundMapp), true); } //client.CheckOut(new Uri("https://svn.momentum.se/svn/PMSkript/Konvertering PM4 - PM5/PM4_Grund_Konvertering/Standard_Uppsättning"), KundMapp); try { files = Directory.GetFiles(KundMapp, "*.sql", SearchOption.AllDirectories); for (int i = 1; i < files.Count(); i++) { StreamReader reading = File.OpenText(files[i]); string str; while ((str = reading.ReadLine()) != null) { if (str.Contains(HittaTxt.Text)) { string test = str.Replace(HittaTxt.Text, ErsättTxt.Text); File.WriteAllText(files[i], test); } } } } catch(IOException exception) { MessageBox.Show(exception.ToString()); }
thanks for all answer!
Answers
-
Do to so you have to read the whole file, check/replace content and then write the file. You can't update the file while reading it.
The following code should do the basics:
foreach (var f in files) { var content = File.ReadAllText(f); if (content.Contains(HittaTxt.Text)) { content = content.Replace(HittaTxt.Text, ErsättTxt.Text); File.WriteAllText(f, content); } }
- Marked as answer by Carlo Goretti Thursday, November 14, 2019 10:42 AM
All replies
-
-
But i want it to replace if it finds the specific word?
- Edited by Carlo Goretti Thursday, November 14, 2019 10:17 AM
-
Then you can for example read content of the file first to StringBuilder. Then replace what needs to be replaced. Then overwrite the file with content of StringBuilder.
Other solution is to read source file, replace text, write to some destination like temporary file and then at the end replace source file.
Quick example from first approach would be something like
public void ReplaceFileContent(string filePath, string replace, string replacement) { StringBuilder sb = new StringBuilder(); using (StreamReader reader = File.OpenText(filePath)) sb.Append(reader.ReadToEnd()); sb.Replace(replace, replacement); using (StreamWriter writer = new StreamWriter(File.OpenWrite(filePath))) { writer.Write(sb.ToString()); writer.Flush(); } }
- Edited by MasaSam Thursday, November 14, 2019 10:28 AM
-
Do to so you have to read the whole file, check/replace content and then write the file. You can't update the file while reading it.
The following code should do the basics:
foreach (var f in files) { var content = File.ReadAllText(f); if (content.Contains(HittaTxt.Text)) { content = content.Replace(HittaTxt.Text, ErsättTxt.Text); File.WriteAllText(f, content); } }
- Marked as answer by Carlo Goretti Thursday, November 14, 2019 10:42 AM
-
-