Exemple #1
0
        private FileLockerPayload setPayload(FILE_LOCKER_MESSAGE_CODE code, string message)
        {
            FileLockerPayload payload = new FileLockerPayload();

            payload.code    = code;
            payload.message = message;

            return(payload);
        }
Exemple #2
0
        public bool lockFile(string filePath)
        {
            FileLockerPayload payload = null;

            if (isLock)
            {
                payload = setPayload(FILE_LOCKER_MESSAGE_CODE.LOCK_ALREADY_BY_PROCESS, FILE_LOCKER_MESSAGE_CODE.LOCK_ALREADY_BY_PROCESS.ToString());
            }
            else
            {
                try
                {
                    _fileLock = filePath;

                    if (!File.Exists(filePath))
                    {
                        File.Create(filePath);
                    }
                    else
                    {
                        stream = File.Open(fileLock, FileMode.Open);
                    }

                    payload = setPayload(FILE_LOCKER_MESSAGE_CODE.SUCCESS, FILE_LOCKER_MESSAGE_CODE.SUCCESS.ToString());

                    _isLock = true;
                }
                catch (Exception ex)
                {
                    payload = setPayload(FILE_LOCKER_MESSAGE_CODE.UNABLE_TO_CREATE_LOCK, ex.Message);
                    _isLock = false;
                }
            }

            if (_enableEvents)
            {
                FileLockerHandler(payload);
            }

            return(_isLock);
        }
Exemple #3
0
        public bool unlockFile()
        {
            if (_isLock)
            {
                if (stream != null)
                {
                    stream.Close();
                    stream.Dispose();
                    stream = null;
                }

                if (_enableEvents)
                {
                    FileLockerPayload payload = setPayload(FILE_LOCKER_MESSAGE_CODE.RELEASE_LOCK, FILE_LOCKER_MESSAGE_CODE.RELEASE_LOCK.ToString());
                    FileLockerHandler(payload);
                }

                _isLock = false;
            }

            return(isLock);
        }
Exemple #4
0
        public bool deleteLockFile()
        {
            FileLockerPayload payload = null;
            bool isSuccess            = false;

            if (_fileLock != string.Empty)
            {
                if (_isLock)
                {
                    payload = setPayload(FILE_LOCKER_MESSAGE_CODE.LOCK_ALREADY_BY_PROCESS, FILE_LOCKER_MESSAGE_CODE.LOCK_ALREADY_BY_PROCESS.ToString());
                }
                else
                {
                    try
                    {
                        File.Delete(_fileLock);
                        isSuccess = true;
                    }
                    catch (Exception ex)
                    {
                        payload   = setPayload(FILE_LOCKER_MESSAGE_CODE.UNABLE_DELETE_LOCK, ex.ToString());
                        isSuccess = false;
                    }
                }
            }
            else
            {
                payload = setPayload(FILE_LOCKER_MESSAGE_CODE.LOCK_NOT_EXISTS, FILE_LOCKER_MESSAGE_CODE.LOCK_NOT_EXISTS.ToString());
            }

            if (_enableEvents)
            {
                FileLockerHandler(payload);
            }

            return(isSuccess);
        }