Esempio n. 1
0
        public bool DeleteFile(string fileName)
        {
            var       filePath = GetFilePath(fileName);
            const int max      = 10;
            int       attempt  = 1;

            while (true)
            {
                try
                {
                    if (!File.Exists(filePath))
                    {
                        return(false);
                    }

                    File.SetAttributes(filePath, FileAttributes.Normal);
                    File.Delete(filePath);
                    return(true);
                }
                catch (IOException e)
                {
                    if (attempt >= max)
                    {
                        var lockProcesses = FileLockerUtil.WhoIsLockingSafe(filePath, out _);
                        if (lockProcesses != null)
                        {
                            throw new Exception($"Can't delete the '{filePath}' file. It is locked by the process '{string.Join(", ", lockProcesses.Select(p => $"[{p.Id}]{p.ProcessName}"))}'", e);
                        }

                        throw;
                    }
                }
                catch (UnauthorizedAccessException e)
                {
                    if (attempt >= max)
                    {
                        var lockProcesses = FileLockerUtil.WhoIsLockingSafe(filePath, out _);
                        if (lockProcesses != null)
                        {
                            throw new Exception($"Can't delete the '{filePath}' file. It is locked by the process '{string.Join(", ", lockProcesses.Select(p => $"[{p.Id}]{p.ProcessName}"))}'", e);
                        }

                        throw;
                    }
                }
                catch (Exception)
                {
                    if (attempt >= max)
                    {
                        throw;
                    }
                }

                attempt++;
                Thread.Sleep(attempt * 100);
            }
        }
Esempio n. 2
0
        public async Task <string> ReadStringAsync(string fileName)
        {
            var filePath = GetFilePath(fileName);
            var fileInfo = new FileInfo(filePath);

            if (!fileInfo.Exists)
            {
                return(null);
            }

            const int max     = 10;
            int       attempt = 1;

            while (true)
            {
                try
                {
                    using (var fileStream = fileInfo.Open(FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
                        using (var fileReader = new StreamReader(fileStream))
                        {
                            return(await fileReader.ReadToEndAsync());
                        }
                }
                catch (IOException e)
                {
                    if (attempt >= max)
                    {
                        var lockProcesses = FileLockerUtil.WhoIsLockingSafe(filePath, out _);
                        if (lockProcesses != null)
                        {
                            throw new Exception($"Can't read the '{filePath}' file. It is locked by the process '{string.Join(", ", lockProcesses.Select(p => $"[{p.Id}]{p.ProcessName}"))}'", e);
                        }

                        throw;
                    }
                }
                catch (UnauthorizedAccessException e)
                {
                    if (attempt >= max)
                    {
                        var lockProcesses = FileLockerUtil.WhoIsLockingSafe(filePath, out _);
                        if (lockProcesses != null)
                        {
                            throw new Exception($"Can't read the '{filePath}' file. It is locked by the process '{string.Join(", ", lockProcesses.Select(p => $"[{p.Id}]{p.ProcessName}"))}'", e);
                        }

                        throw;
                    }
                }
                catch (Exception)
                {
                    if (attempt >= max)
                    {
                        throw;
                    }
                }

                attempt++;
                await Task.Delay(attempt * 100);
            }
        }