Esempio n. 1
0
        internal static void PrepareBrand(EM_UI_MainForm emptyForm)
        {
            try
            {
                string brandName  = GetProjectNameOrDefault();
                string brandTitle = Properties.Resources.ResourceManager.GetString("brand_title_" + brandName);

                if (String.IsNullOrEmpty(brandTitle))
                {
                    brandTitle = brandName = "EUROMOD";
                    // For now, completely go silent if the brand is wrong. To warn, uncomment the line below:
                    //UserInfoHandler.ShowError($"File '{projectSettingsFullPath}' does not contain a valid Project Name.{Environment.NewLine}Alternative brand is ignored.");
                }

                DefGeneral.BRAND_TITLE = brandTitle;
                DefGeneral.BRAND_NAME  = brandName;
                SetBackgroundImage(emptyForm);
                SetIcon(emptyForm);
                emptyForm.btnRun.Caption            = $"Run{Environment.NewLine}{DefGeneral.BRAND_TITLE}";
                emptyForm.barText_PoweredBy.Caption = DefGeneral.IsAlternativeBrand() ? GetPoweredByText() : string.Empty;
            }
            catch (Exception exception)
            {
                UserInfoHandler.ShowException(exception, "Adapting to alternative Brand failed!", false);
            }
        }
        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);
        }
        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
        }
 internal static void ReleaseFile(string fullFileName)
 {
     try
     {
         string inUseFilePath = GetInUseFilePath(fullFileName);
         if (File.Exists(inUseFilePath))
         {
             File.Delete(inUseFilePath);
         }
     }
     catch (Exception exception)
     {
         UserInfoHandler.ShowException(exception);
     }
 }
        //Helper
        internal static bool Folder(string sourceFolderPath, string destinationFolderPath, string renameFolderTo = "")
        {
            bool undo = false;

            if (!FolderRecursivly(sourceFolderPath, destinationFolderPath, ref undo, renameFolderTo))
            {
                if (!undo)
                {
                    return(false);
                }
                try
                {
                    //undo any copying that already happened
                    DirectoryInfo sourceFolder = new DirectoryInfo(sourceFolderPath);
                    if (renameFolderTo == string.Empty)
                    {
                        destinationFolderPath = EMPath.AddSlash(destinationFolderPath + sourceFolder.Name);
                    }
                    else
                    {
                        destinationFolderPath = EMPath.AddSlash(destinationFolderPath + renameFolderTo);
                    }

                    if (Directory.Exists(destinationFolderPath))
                    {
                        Directory.Delete(destinationFolderPath, true);
                    }
                }
                catch (Exception exception)
                {
                    UserInfoHandler.ShowException(exception);
                }
                return(false);
            }
            return(true);
        }