System.Security.Securityexception [RESOLVED]
I have a service that uses the fso to copy files to different directories and I am getting the following intermittent error:
Exception from HRESULT: 0x800A0046 (CTL_E_PERMISSIONDENIED)
I searched MSDN and found that the exception can be caused by file or folder permissions being improperly set, or the file can be in use by another process. I have caught the error. And, I believe that the files that cause the error are in use by another process because if I stop and restart the service they copy perfectly.
Here's are my questions: How can I find out what process is denying access to the files? And, how should I handle the exception?
I found a post with this title: fso.movefile permission denied that had a similar situation.
I tried everything in that post and nothing worked. I don't have a streamreader open. I'm still getting the error occassionally.
Any help is more than appreciated!
Re: System.Security.Securityexception
Re: System.Security.Securityexception
Should I not be using the file system object in .net?
Re: System.Security.Securityexception
I'm not sure. I'm brand new to .net. But, I changed the code to System.IO.File.Copy and got the same results. :confused:
Re: System.Security.Securityexception
I'm sure this is not good form, but here is what I did...
In the Try Catch block where I get the CTL_E_PERMISSION denied in the catch statement I do this:
System.IO.File.Copy(Path & filename, ds.Tables("MaskPath").Rows(x)("DestinationPath"), False)
Now I'm getting an error which source is mscorlib and the message is "The target directory already exists.) I don't understand. I'm sure I'm doing something stupid. But, please be gentle!!! :bigyello:
Re: System.Security.Securityexception (STILL UNRESOLVED)
I've tried copying and moving the file using System.IO.File.Move(Path, Path2)
and System.IO.File.Copy(Path,Path2,True) instead of using fso and I'm still getting the error "The process cannot access the file because it is being used by another process."
The error is intermittent. The program is a service so I can't step through it. I have debug statements written throughout the code. I don't know how to resolve this. Any help?
I've searched the web and see that this is not an uncommon problem but I have been unable to find a suitable answer.
Re: System.Security.Securityexception (STILL UNRESOLVED)
what file is it you are trying to move??? that may help to determine WHAT process is currently using the file...
is this a file your app generates? or something like a logfile another app generates...
also, generall doing a file copy versus a move will succeed, because it can keep the current file in its place (for the process that is locking it) and copy it to your destination... this of course depends on if you NEED it to be moved and not copied...
Re: System.Security.Securityexception (STILL UNRESOLVED)
I'm using a file system watcher to watch for new files that are written to a source directory and then copying them to predetermined destination directories. This is sales data coming from stores, and the files are of various types. Never log files. I'm getting this error very intermittently and I can't figure out what process is causing the lock.
Re: System.Security.Securityexception (STILL UNRESOLVED)
well what creates the files... I don't know how it is setup.. but if I was in that situation I would suspect that you are monitoring for a file, lets say its called sales.txt
when sales.txt changes, your filesystemwatcher notices, and does what you need the code to do (copy/move the file to another location)
what is probably happening, is whatever program creates this sales.txt, it still has sales.txt open for write access when your app tries to move the file...
you probably need a way to determine if the file you need to grab has been finished being created.. the process or creating the files is probably pretty quick, which is why sometimes this problem happens and sometimes it doesnt... sometimes the file has been closed by the time your code goes to move it, other times it is still being written to when you try...
this is only a guess.. but it is something to look at...
Re: System.Security.Securityexception (STILL UNRESOLVED)
You are right about the process.
I thought about there being a time conflict so I put a time stamp in the debug code and then compared it with the files that were copied thinking I'd see a small variation in time in most cases and then on files that couldn't be copied there would be no variation because I was trying to copy them at the same time as they were being written to. But that wasn't the case. The timestamps were exactly the same time in all cases whether there was a conflict copying the file or not. But, I'm going to give this another look.
Re: System.Security.Securityexception (STILL UNRESOLVED)
basically what you should do is, when you catch the error that you are having trouble with, write code to have it wait 30 seconds and try again to copy said file.. or something along those lines.. once the file is available (freed from the processing that was writing it) you can copy it.. or perhaps this file is ALWAYS OPEN by the process that writes to it????? if that was the case, then a copy instead of move, should work...
Re: System.Security.Securityexception (STILL UNRESOLVED)
I put the time code in there. In your last post, when you said a copy should work... I am using copy. I tried copy and move. Are you saying it should allow me to copy even if another process has access to the file?
Re: System.Security.Securityexception [RESOLVED]
Kleinma - YOU ARE A GENIUS! The timer worked. I had actually thought about doing something similar, but thought that the file could be locked longer than whatever time I chose to wait... But this is good enough for now!
Thanks for all your help!
Re: System.Security.Securityexception [RESOLVED]
no problem... you are right that there are probably tighter ways to get the code, you know to handle things better... but sometimes the biggest step is to just get code to work... then you can clean it up, and make it more efficient later on (especially if you have a deadline :bigyello: )