Ejemplo n.º 1
0
        private static void CheckFileLock()
        {
            try
            {
                var file = SelectFilesDialog(multiselect: false).FirstOrDefault();

                if (file == null)
                {
                    return;
                }

                var lockers = FileLockChecker.WhoIsLocking(file);

                var sb = new System.Text.StringBuilder();
                foreach (var proc in lockers)
                {
                    using (proc)
                        sb.Append("ID: ").Append(proc.Id.ToString()).Append(" | ").Append("Name: ").AppendLine(proc.ProcessName);
                }

                MessageBox.Show(sb.Length == 0 ? "Файл не занят" : sb.ToString(), "Блокирующие процессы");
            }
            catch (Exception ex)
            {
                LogAndShow("Возникло исключение при проверке блокировки файла", ex);
            }
        }
Ejemplo n.º 2
0
        internal override bool SaveFile(MemoryStream stream, string filename)
        {
            // Not implemented in SevenZipArchive...
            if (isreadonly || archivetype == ArchiveType.SevenZip)
            {
                return(false);
            }

            // Convert slashes...
            filename = filename.Replace(Path.DirectorySeparatorChar, Path.AltDirectorySeparatorChar);

            // Check if target file is locked...
            var checkresult = FileLockChecker.CheckFile(location.location);

            if (!string.IsNullOrEmpty(checkresult.Error))
            {
                string errmsg = "Unable to save file \"" + filename + "\" into archive \"" + location.GetDisplayName() + "\".";
                if (checkresult.Processes.Count > 0)
                {
                    string processpath = string.Empty;
                    try
                    {
                        // All manner of exceptions are possible here...
                        processpath = checkresult.Processes[0].MainModule.FileName;
                    }
                    catch { }

                    errmsg += " Archive is locked by " + checkresult.Processes[0].ProcessName
                              + " (" + (!string.IsNullOrEmpty(processpath) ? "\"" + processpath + "\"" : "")
                              + ", started at " + checkresult.Processes[0].StartTime + ").";
                }

                General.ErrorLogger.Add(ErrorType.Error, errmsg);
                return(false);
            }

            using (MemoryStream savestream = new MemoryStream())
            {
                using (ZipArchive za = (ZipArchive)ArchiveFactory.Open(location.location))
                {
                    if (za == null)
                    {
                        string errmsg = "Unable to save file \"" + filename + "\" into archive \"" + location.GetDisplayName() + "\". Unable to open target file as a zip archive.";
                        General.ErrorLogger.Add(ErrorType.Error, errmsg);
                        return(false);
                    }

                    // Find and remove original entry...
                    foreach (ZipArchiveEntry entry in za.Entries)
                    {
                        if (!entry.IsDirectory && entry.Key == filename)
                        {
                            za.RemoveEntry(entry);
                            break;
                        }
                    }

                    // Add new entry and save the archive to stream...
                    za.AddEntry(filename, stream, 0L, DateTime.Now);
                    za.SaveTo(savestream, CompressionType.Deflate);
                }

                // Replace archive file...
                File.WriteAllBytes(location.location, savestream.ToArray());
            }

            // Rewind the stream (because DirectoryReader/WADReader don't modify stream Position in SaveFile)
            stream.Position = 0;

            return(true);
        }