st mnmn
st mnmn

Reputation: 3677

documentBuilder: The process cannot access the file because it is being used by another process

I am using DocumentBuilder (of openXML api), to those who doesn't know the documentBuilder I'll give a short explanation: it has a function 'BuildDocument' which gets list of sources (each source contains wmldocument), and string of fileName to save to.

    public static void BuildDocument(List<Source> sources, string fileName)

the purpose of this function is to build one word docx which contains all the sources. it merges some docs to one. at the end of its functionality it saves the doc using:

    File.WriteAllBytes(...)

but when I run my project on the server I keep getting the error: "The process cannot access the file because it is being used by another process." few times it works ok. and in the visualStudio it also works without errors. what can be the problem?

Upvotes: 0

Views: 1957

Answers (3)

st mnmn
st mnmn

Reputation: 3677

Ok, I found my mistake, it was in my code and not in the DocumentBuilder. I wrote:

    FileInfo f=new FileInfo....
    f.Create();

and I didn't close it. I had to change the code to: FileInfo f=new FileInfo.... FileStream fs=f.Create(); fs.Close(); Thank you all for your will to help me!!! I realy appreciate that!

Upvotes: 0

vpv
vpv

Reputation: 938

After saving the saving file, check whether it has been closed properly. If it is not, then next time when you try to open the file and add text into it, you may get the error. Use something like File.Close()

Upvotes: 0

Dejo
Dejo

Reputation: 2148

Probably document file is already open.

Upvotes: 1

Related Questions