Example #1
0
        static bool ImportFile(string[] args)
        {
            if (args[0] == "?")
            {
                Console.WriteLine("Imports file into current directory.");
                Console.WriteLine("import source filename [-o]");
                Console.WriteLine("-o - Overwrite enabled.");
                return(false);
            }

            string source    = args[0];
            string filename  = args[1];
            bool   overwrite = args.Length > 2 ? args[2] == "-o" : false;

            IFileInfo[] files = currentDirectory.GetFiles();
            for (int i = 0; i < files.Length; i++)
            {
                if (files[i].Name.Trim('\0') == filename)
                {
                    try
                    {
                        if (overwrite)
                        {
                            files[i].WriteAllBytes(File.ReadAllBytes(source));
                            files[i].Create();
                            return(true);
                        }
                        else
                        {
                            Console.WriteLine("File already exists.");
                            return(false);
                        }
                    }
                    catch (Exception ex)
                    {
                        Console.WriteLine("ERROR: " + ex.Message);
                        return(false);
                    }
                }
            }

            // If file is new
            IFileInfo file = currentDirectory.CreateFile(filename);

            try
            {
                file.WriteAllBytes(File.ReadAllBytes(source));
                file.Create();
                return(true);
            }
            catch (Exception ex)
            {
                Console.WriteLine("ERROR: " + ex.Message);
                return(false);
            }
        }