File lock problem

G

Grant

Occasionally when I try and save a document:

-----------------------
xmlDoc.Save(Application.StartupPath + @"\Config.xml");
-----------------------

I get this error saying that the file could not be accessed because its
locked by another process or something (I cant get it to do it now - because
I want it to crash...)
Is there a way to check whether a file is locked by another process befre
trying to save it? If it is locked is there a way to terminate the other
process or somewhow free it up so that it can be saved?

Thanks,
Grant
 
C

CheshireCat

I can't answer your specific question but if I try to open a file I use
try/catch and usually loop the process over a short time period with some
thread.sleep(0) thrown in.
 
I

Ignacio Machin \( .NET/ C# MVP \)

Hi,

No, AFAIK there is no way to know if a file is locked beforehand, you have
to open it and it will give you error, use a try/catch block .

It's possible to know what process is using a given file, filemon from
sysinternals.com does it. how ? frankly I have no idea , IIRC there is no a
easy way to get this info. of course there is not a managed way of doing it.


cheers,
 
G

Grant

I can open the file ok and make modifications - its when I do the save bit
that it fails - is this where I would expect it to fail or when Im opening
the file?

Either way though Ill try open the file in a try catch block on a seperate
thread (now to learn how to do this on a seperate thread...)

Thanks for your help.

Grant
 
W

Willy Denoyette [MVP]

Grant said:
I can open the file ok and make modifications - its when I do the save bit
that it fails - is this where I would expect it to fail or when Im opening
the file?

Either way though Ill try open the file in a try catch block on a seperate
thread (now to learn how to do this on a seperate thread...)

Thanks for your help.

Grant

That means you still have your file open on the other thread, right?
If that's true, are you sure the file is opened for shared RW access?

Willy.
 
C

Cor Ligthert

Grant,

What you ask has never been possible with any (regular) Microsoft OS.

It would be very nice when we could.

They have build themselves a special feature for that in Office.

Cor
 
G

Grant

how do you get a return value from a seperate thread? Ive got this far but I
dont know how to check whether the update was a success or not..

Thread updaterThread = new Thread(new ThreadStart(updateSomething));
updaterThread.Start();

Thanks,
Grant
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top