none
Cannot access the file because it being used by another process.. RRS feed

  • 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!
    Thursday, November 14, 2019 9:26 AM

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
    Thursday, November 14, 2019 10:32 AM

All replies

  • You are reading file and trying to write it also at same time.

    File.OpenRead opens the file for reading, then at File.WriteAllText you try to write to same file.

    Thursday, November 14, 2019 10:11 AM
  • But i want it to replace if it finds the specific word?
    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
    Thursday, November 14, 2019 10:26 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
    Thursday, November 14, 2019 10:32 AM
  • Tried this now but i get Access to the filepath is denied..
    Thursday, November 14, 2019 10:37 AM
  • This one worked!
    Thursday, November 14, 2019 10:43 AM