Our Products:   CompleteFTP  edtFTPnet/Free  edtFTPnet/PRO  edtFTPj/Free  edtFTPj/PRO
0 votes
2.3k views
in .NET FTP by (140 points)
How can I tell if a file is locked?

Im using FTP to move log files.. but Im not sure how to tell if the server is still writing to the file.... Is there some way to determine if a file is locked (i.e. being written to... or held open by a process that may intend to write to it in the future)

Just for reference here is what Im doing now:


string BlankIfNotLockedOtherwiseFileIsLocked = ""

try {
ftp.Rename(currentFile.Name,currentFile.Name);
}catch(Exception err){
// If there is a file lock error the ReplyText will be:
// ReplyText: @[filename]: The process cannot access the file because it is being used by another process.
// The ReplyCode will be: 550
BlankIfNotLockedOtherwiseFileIsLocked = err.ToString();
}

if (BlankIfNotLockedOtherwiseFileIsLocked = ""){
// import the file
}else{
// leave it alone, its locked
}

This really seems like a bad way to do things... its a hack.. What I really want to do is just read some "isLocked" property.. or parse that out of some file information somewhere....

1 Answer

0 votes
by (162k points)
No, there is no simple way to tell (unless the FTP server is smart enough to handle locking).

How can I tell if a file is locked?

Im using FTP to move log files.. but Im not sure how to tell if the server is still writing to the file.... Is there some way to determine if a file is locked (i.e. being written to... or held open by a process that may intend to write to it in the future)

Categories

...