Want more features on Pastebin? Sign Up, it's FREE!
Guest

System.IO.IOException: The process cannot access the file 'file_name'

By: a guest on Feb 22nd, 2012  |  syntax: None  |  size: 1.50 KB  |  views: 16  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. System.IO.File.Copy(path, ConfigurationManager.AppSettings["BulkInsertGiftRegisterCreatorDirectory"] + System.IO.Path.GetFileName(path));
  2.  
  3. Loyalty.Entity.Base.FileInfo file = new Loyalty.Entity.Base.FileInfo();
  4. file.FileName = path;
  5. request.Object = file;
  6.  
  7. ResponseBase response = new ResponseBase(request);
  8. RequestConnection connection = new RequestConnection("cn");
  9. FileManager fileManager = new FileManager(request, connection);
  10. response = fileManager.OfflineGiftRegisterBulkInsert();
  11.  
  12. System.IO.File.Delete(ConfigurationManager.AppSettings["BulkInsertGiftRegisterCreatorDirectory"] + System.IO.Path.GetFileName(path));
  13.  
  14. // here is the part of stored procedure that uses file
  15. SELECT @SCRIPT= 'BULK INSERT GIFT_CARD.GIFT_TEMP'
  16.                             +' FROM '''
  17.                             + @FILE_PATH
  18.                             +''' WITH ('
  19.                                  +'FIELDTERMINATOR = '','','
  20.                                  + 'KEEPNULLS'
  21.                             +');'
  22.        
  23. public static System.Boolean FileInUse(System.String file)
  24. {
  25.     try
  26.     {
  27.         if (!System.IO.File.Exists(file)) // The path might also be invalid.
  28.         {
  29.             return false;
  30.         }
  31.  
  32.         using (System.IO.FileStream stream = new System.IO.FileStream(file, System.IO.FileMode.Open))
  33.         {
  34.             return false;
  35.         }
  36.     }
  37.     catch
  38.     {
  39.         return true;
  40.     }
  41. }
  42.        
  43. public static void WaitForFile(System.String file)
  44. {
  45.     // While the file is in use...
  46.     while (FileInUse(file)) ; // Do nothing.
  47. }
clone this paste RAW Paste Data