XML processing exception

I will just get back to my question here (not yet resolved):

C # Exception Process cannot access file

An exception does not occur when I work in debug mode, it only does it when I start from exe.

Can someone explain to him the reason why he gives an exception when starting exe, and not in debug mode.

The first time I run exe, it works successfully and gives me the XML file that I need. But for the second find for the observer. This gives me this exception: the process cannot access the file.

+2
source share
4 answers

I looked through this complete set of codes ... the problem you are facing is related to events and time. Your event is fired from a FileWatcher object before any process saves the file. Try putting Thread.Sleep right at the top of the Convert method ... don't take too long, start maybe in 1 second or so ... see what happens.

private static void convert(object source, FileSystemEventArgs f) { string FileName; FileName = f.FullPath; string FilePath; FilePath = f.Name; var watcher = source as FileSystemWatcher; string destinationFile = @"D:/GS/" + FilePath; Thread.Sleep(1000); //... 
+3
source

I think you did the wrong lock, and therefore several threads are trying to get your file

Try the following:

 if (Monitor.TryEnter(lockObject)) { try{ //Your actual code } } else return; 
+3
source

The exe process runs continuously. Your exe process should probably be stopped. Then the error "The process cannot access the file" will never appear. Use the correct error handling methods for your codes. Your code has a continuous link to the XML file. So, if you try to access a second time, an access error is suitable if I am not mistaken.

+1
source

Here is the final convert () that works correctly. Thanks to everyone who helped me here. I really appreciate your time and effort to help me. I would like to agree with these decisions above. But I give my vote to both of you. Thanks again.

 private static void convert(object source, FileSystemEventArgs f) { string FileName; FileName = f.FullPath; string FilePath; FilePath = f.Name; var watcher = source as FileSystemWatcher; string destinationFile = @"D:/GS/" + FilePath; System.Threading.Thread.Sleep(1000); if (Monitor.TryEnter(lockObject)) ** { try { watcher.EnableRaisingEvents = false; XmlDocument xdoc = new XmlDocument(); try { xdoc.Load(FileName); xdoc = null; } catch (XmlException xe) { xdoc = null; using (StreamWriter w = File.AppendText(FileName)) { Console.WriteLine(xe); w.WriteLine("</title>"); w.WriteLine("</titleContent>"); Console.WriteLine("1st"); } } System.Threading.Thread.Sleep(2000); XPathDocument myXPathDoc = new XPathDocument(new StreamReader(FileName, System.Text.Encoding.GetEncoding("windows-1256"))); XslCompiledTransform myXslTrans = new XslCompiledTransform(); myXslTrans.Load("D:/GS/xsl/test.xsl"); XmlTextWriter myWriter = new XmlTextWriter(destinationFile, null); myWriter.Formatting = Formatting.Indented; myWriter.Indentation = 4; myXslTrans.Transform(myXPathDoc, null, myWriter); myWriter.Close(); Console.WriteLine("2nd"); } catch (Exception e) { Console.WriteLine("The process failed: {0}", e.ToString()); } finally { Monitor.Exit(lockObject); watcher.EnableRaisingEvents = true; Console.WriteLine("Finally"); GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); } } } 

}

0
source

All Articles