Esempio n. 1
0
        public static void CreateTemplate(string path = null)
        {
            try
            {
                if (path == null)
                {
                    path = "TemplateFile" + Path.GetExtension("*.cs");
                }
                CFormat.WriteLine("Creating template file \"" + path + "\"");
                if (File.Exists(path))
                {
                    CFormat.WriteLine("A file named \"" + path + "\" already exists. Replace it?");
                    ConsoleAnswer answer = CInput.UserChoice(ConsoleAnswerType.YesNo, true);
                    if (answer == ConsoleAnswer.No)
                    {
                        int    num = 1;
                        string withoutExtension = Path.GetFileNameWithoutExtension(path);
                        while (File.Exists(path))
                        {
                            path = Path.Combine(Path.GetDirectoryName(path), withoutExtension + " (" + num + ")", Path.GetExtension(path));
                            ++num;
                        }
                    }
                    else if (answer == ConsoleAnswer.Escaped)
                    {
                        return;
                    }
                }

                using (StreamWriter streamWriter = new StreamWriter(path, false, Encoding.UTF8))
                    streamWriter.Write(Properties.Resources.FileTemplate);

                CFormat.WriteLine("Template file named \"" + path + "\" created!", ConsoleColor.Green);
            }
            catch (Exception ex)
            {
                CFormat.WriteLine("Could not create template file \"" + path + "\" Details: " + ex.Message, ConsoleColor.Red);
            }
        }