Click here to Skip to main content
Rate this: bad
good
Please Sign up or sign in to vote.
See more: C# ASP.NET
Hi Experts,
 
My project is to generate word documents which is in the server. If I click the generate button, a word document will be generated where the file will be copied to a new document.
 
The issue I am facing here is , when i click generate and if some one else is also trying to use the portal to generate a document, then they ll get a message like "The file is already opened by someone else". and if they open after a mins, then they ll be able to copy the file..
This is the code i used ..
public void UpdateTOC(string file1, string file2)
     {
         using (var mainDoc = WordprocessingDocument.Open(file1, false))
         using (var resultDoc = WordprocessingDocument.Create(file2, WordprocessingDocumentType.Document))
         {
             // copy parts from source document to new document
             foreach (var part in mainDoc.Parts) resultDoc.AddPart(part.OpenXmlPart, part.RelationshipId);
             // perform replacements in resultDoc.MainDocumentPart   // ... }
         }
 
     }
 
Now, I don want that message to display. Is there anyother option to overcome such an issue.. Any other code snippets would also be helpfull..
 
Please help,
Thanks,
Anusha.
Posted 26-Sep-12 19:53pm
Edited 26-Sep-12 21:01pm
v2

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

On server's because they are multi-user it is best to use unique names for you file when you are creating them so you don't get these error messages.
 
The easiest way is to use Guid.NewGuid().ToString() for a filename.
  Permalink  
Comments
Anusha Sridhar at 27-Sep-12 2:38am
   
hi, thanks for the reply.. Can u pls help me with the code snippet.
Thanku.
Mehdi Gholam at 27-Sep-12 2:44am
   
Just use the red statement above as your filename.
Anusha Sridhar at 27-Sep-12 3:33am
   
I can understand what you are saying.. But I have the base document which is same. If a person clicks generate, then the document will be saved with the base document name along with their ID.. Is it possible to write this code using thread??
Mehdi Gholam at 27-Sep-12 3:45am
   
Copy the template file to the Guid.NewGuid() filename then operate on that file.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
0 Sergey Alexandrovich Kryukov 7,442
1 OriginalGriff 5,996
2 Peter Leow 4,292
3 CHill60 2,858
4 Mika Wendelius 2,850


Advertise | Privacy | Mobile
Web03 | 2.8.150224.1 | Last Updated 27 Sep 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100