public static async Task WriteToLog(NotifyFilters?notifyFilter, CopyDiagnostics copyDiagnostics)
        {
            // TODO: Enforce the users log settings.
            Settings defaultSettings = await new SettingsRepository().FirstOrDefaultAsync();

            string logAddendum = string.Empty;

            if (notifyFilter != null)
            {
                logAddendum = $"{System.Enum.GetName(typeof(NotifyFilters), notifyFilter)},{copyDiagnostics.SourcePath},{copyDiagnostics.TargetPath},{copyDiagnostics.ElapsedTime} (ms),{DateTime.Now}";
            }
            else
            {
                logAddendum = $"VS Cache File,{copyDiagnostics.SourcePath},{copyDiagnostics.TargetPath},{copyDiagnostics.ElapsedTime} (ms),{DateTime.Now}";
            }

            using (FileStream fileStream = new FileStream(Constants.LogFilePath, FileMode.Append))
            {
                byte[] logAddendumBytes = new UTF8Encoding(true).GetBytes(logAddendum);
                await fileStream.WriteAsync(logAddendumBytes, 0, logAddendumBytes.Length);

                byte[] newline = Encoding.ASCII.GetBytes(Environment.NewLine);
                await fileStream.WriteAsync(newline, 0, newline.Length);
            }
        }
        private void ContextItemForceCopy_Click(object sender, EventArgs e)
        {
            string          _symbolicLinkName = listSymbolicLinks.FocusedItem.Text;
            CopyDiagnostics copyDiagnostics   = this.SymbolicLinkUtil.SyncLinkedDirectory(_symbolicLinkName);

            this.AddHistoryItem($"Directory contents copied from {copyDiagnostics.SourcePath} to {copyDiagnostics.TargetPath} ({copyDiagnostics.ElapsedTime} ms)", DirWatchTransferApp.StatusInformationImageConfig.ImageIndex);
        }
Exemple #3
0
        public async Task StartWatcher(int watcherID)
        {
            bool isTripped = false;

            Watcher watcher = await this.watcherRepo.FirstOrDefaultAsync(a => a.ID == watcherID);

            SymbolicLink symbolicLink = await this.symbolicLinkRepo.FirstOrDefaultAsync(a => a.ID == watcher.SymbolicLinkID);

            System.Tuple <int, int> monitorKey = new System.Tuple <int, int>(watcher.SymbolicLinkID, watcher.ID);

            if (DirWatcherTransferApp.Monitors.ContainsKey(monitorKey))
            {
                throw new System.Exception("Watcher already has monitor running. Please end the existing monitor before starting a new one.");
            }

            FileSystemMonitor fileSystemMonitor = new FileSystemMonitor();

            // This actions gets fired when a file changes.
            fileSystemMonitor.CopyCompletedAction = async(notifyFilter, fileSystemEventArgs) =>
            {
                // TODO: Implement a better way to check for visual studio temp files. This will fail if the actual file have a "~" in the name.
                if (!isTripped && fileSystemEventArgs.Name.Contains("~") && !fileSystemEventArgs.Name.EndsWith("~"))
                {
                    isTripped = true;

                    string fileName = fileSystemEventArgs.Name;
                    string filePath = fileSystemEventArgs.FullPath;

                    if (fileSystemEventArgs.Name.Contains("~") && !fileSystemEventArgs.Name.EndsWith("~"))
                    {
                        // Remove the vs caching name format.
                        fileName = fileSystemEventArgs.Name.Remove(fileSystemEventArgs.Name.LastIndexOf("~"));
                        filePath = fileSystemEventArgs.FullPath.Replace(fileSystemEventArgs.Name, fileName);
                    }

                    // Sync the changed file with the target file and update the SignalR clients of the copied file.
                    CopyDiagnostics copyDiagnostics = await this.SyncLinkedFile(fileName, filePath);

                    await this.fileSystemHubContext.Clients.All.SendAsync("onFileCopied", copyDiagnostics);

                    await LogUtility.WriteToLog(notifyFilter, copyDiagnostics);

                    // Update counts.
                    if (notifyFilter != null)
                    {
                        await this.symbolicLinkRepo.IncrementCount(fileSystemEventArgs.FullPath.Replace(@"\" + fileSystemEventArgs.Name, string.Empty), (NotifyFilters)notifyFilter);

                        await this.watcherRepo.IncrementCount(watcher, (NotifyFilters)notifyFilter);
                    }

                    isTripped = false;
                }
            };

            fileSystemMonitor.StartWatcher(symbolicLink.Source, DirWatcherTransferApp.ProcessWatcherFiltersAsList(watcher));
            DirWatcherTransferApp.Monitors.Add(monitorKey, fileSystemMonitor);
        }
        // this.AddHistoryItem($"Link created copy of {copyDiagnostics.SourcePath} at {copyDiagnostics.TargetPath} ({copyDiagnostics.ElapsedTime} ms)", DirWatchTransferApp.LinkImageConfig.ImageIndex);

        #endregion

        #region Callbacks

        private void OnCopyCompleted(CopyDiagnostics copyDiagnostics)
        {
            this.AddHistoryItem($"File contents synced between {copyDiagnostics.SourcePath} and {copyDiagnostics.TargetPath} ({copyDiagnostics.ElapsedTime} ms)", DirWatchTransferApp.LinkImageConfig.ImageIndex);
        }