How to Detach and Re-attach a File

Explains how to detach and re-attach a file with the File Store.

The file associated with a file store may be detached from the file store and later re-attached. This behaviour is useful in cases where the file needs to be closed and later re-opened ; for example, to give up a write lock for backup purposes.

It is very important that the contents of the file should not change while it is detached.

In the following code fragment, the file associated with the file store is detached and closed; the file is then re-opened and re-attached to the file store.

  1. Use the File() member function to retrieve a copy of the RFile object from the file store.

  2. Use the Detach() member function to detach the file store from the file and, in effect, give up ownership of the file.

  3. Use the Reattach() member function to re-attach a file to the file store and, in effect, take ownership of that file.

      
       
      
      LOCAL_C void doUseL(const TDesC& aName)
    {
    TParse filestorename;
    ...
    fsSession.Parse(aName,filestorename);
    CFileStore* store = CPermanentFileStore::OpenLC (fsSession, filestorename.FullName(),EFileRead|EFileWrite );
    RFile thefile = store->File();
    store->Detach();
    thefile.Close();
    ...
    thefile.Open(fsSession, filestorename.FullName(),EFileRead|EFileWrite );
    store->Reattach(thefile);
    ...
    }