0

I am using FileHelper for exporting the data to csv for my C# project. I am facing an exception

"The process cannot access the file"

the reason is the file is already opened. Is there any option to check whether the file is already opened or not.

FileHelperEngine engine = new FileHelperEngine(typeof(ClassName));
engine.WriteFile("file.csv", obj);

Thanks in advance.

Soner Gönül
  • 97,193
  • 102
  • 206
  • 364
Praveen
  • 55,303
  • 33
  • 133
  • 164
  • @Janes Abou Chleih: I'm trying to find a way using FileHelper library. – Praveen May 08 '13 at 06:03
  • @Soner Gönül: Thanks for your edits. – Praveen May 08 '13 at 06:09
  • Oh yeah, you're right. When you catch the exception, don't you automatically know that the file is in use? something like: `using(FileHelperEngine engine = new FileHelperEngine(typeof(ClassName))){ try{ engine.WriteFile("file.csv", obj); }catch{ //File is in use } }` – jAC May 08 '13 at 06:14
  • You should just catch the exception and respond appropriately. Checking if a file is in use before opening it can lead to a race condition because some other process could open it inbetween you checking if it's open and you trying to open it, so you'd still need to deal with potential exceptions anyway. – Matthew Watson May 08 '13 at 06:17
  • @JanesAbouChleih,@MatthewWatson: Thanks. But I thought that FileHelpers has some options to handle this exception. – Praveen May 08 '13 at 06:26

0 Answers0