コード例 #1
0
        internal static bool CaptureFile(string fullFileName, ref bool openedReadOnly)
        {
            try
            {
                openedReadOnly = false;
                string inUseFilePath = GetInUseFilePath(fullFileName);
                if (File.Exists(inUseFilePath))
                {
                    string       usedBy; using (StreamReader reader = new StreamReader(inUseFilePath)) usedBy = reader.ReadLine();
                    DialogResult userDecision = UserInfoHandler.GetInfo(usedBy + Environment.NewLine +
                                                                        "Do you want to open this country file in read-only mode?" + Environment.NewLine + Environment.NewLine +
                                                                        "Click 'Yes' to open in read-only mode." + Environment.NewLine +
                                                                        "If you know this lock has been caused by an error, click 'No' to unlock it and open in read-write mode." + Environment.NewLine +
                                                                        "Click 'Cancel' if you do not want to open this country file." + Environment.NewLine, MessageBoxButtons.YesNoCancel);
                    if (userDecision == DialogResult.Cancel)
                    {
                        return(false);
                    }
                    if (userDecision == DialogResult.Yes)
                    {
                        openedReadOnly = true;
                        return(true);
                    }
                }

                using (StreamWriter writer = new StreamWriter(inUseFilePath))
                    writer.WriteLine(fullFileName + " is currently being edited by user '" + System.Environment.UserName + "'.");
            }
            catch (Exception exception)
            {
                UserInfoHandler.ShowException(exception);
            }
            return(true); //do not stop opening, just because something goes wrong with lock-file-generating
        }
コード例 #2
0
        static bool FolderRecursivly(string sourceFolderPath, string destinationFolderPath, ref bool undo, string renameFolderTo = "")
        {
            sourceFolderPath      = EMPath.AddSlash(sourceFolderPath);
            destinationFolderPath = EMPath.AddSlash(destinationFolderPath);

            try
            {
                DirectoryInfo sourceFolder = new DirectoryInfo(sourceFolderPath);
                if (renameFolderTo == string.Empty)
                {
                    destinationFolderPath = EMPath.AddSlash(destinationFolderPath + sourceFolder.Name);
                }
                else
                {
                    destinationFolderPath = EMPath.AddSlash(destinationFolderPath + renameFolderTo);
                }

                //check if trying to copy in same parent folder
                if (destinationFolderPath.ToLower() == sourceFolderPath.ToLower())
                {
                    UserInfoHandler.ShowError("Cannot duplicate folder '" + sourceFolder.Name + "' in its parent folder.");
                    return(false);
                }

                //check if source folder already exists at destination path
                if (Directory.Exists(destinationFolderPath))
                {
                    if (UserInfoHandler.GetInfo("Folder '" + destinationFolderPath + "' already exists. Should it be deleted?", MessageBoxButtons.OKCancel) == DialogResult.Cancel)
                    {
                        return(false);
                    }
                    Directory.Delete(destinationFolderPath, true);
                }

                //create source folder at destination path
                Directory.CreateDirectory(destinationFolderPath);
                undo = true;

                //copy all files directly under this folder
                FileInfo[] subFiles = sourceFolder.GetFiles("*.*");
                if (subFiles != null)
                {
                    foreach (FileInfo subFile in subFiles)
                    {
                        File.Copy(subFile.FullName, destinationFolderPath + subFile.Name);
                    }
                }
                //find all subfolders under this folder for recursive call
                foreach (DirectoryInfo subFolder in sourceFolder.GetDirectories())
                {
                    FolderRecursivly(subFolder.FullName, destinationFolderPath, ref undo);
                }
            }
            catch (Exception exception)
            {
                UserInfoHandler.ShowException(exception, "Close and reopen the user interface and try again.");
                return(false);
            }
            return(true);
        }