Ejemplo n.º 1
0
        static void Main(string[] args)
        {
            Application.EnableVisualStyles();
            Application.SetCompatibleTextRenderingDefault(false);

            if (args.Length <= 1)
            {
                ShowHelp();
                return;
            }

            // get the arguments
            var mp3Path    = args[0];
            var imagePaths = args.Skip(1).ToArray();

            if (Path.GetExtension(mp3Path).ToLower() != ".mp3")
            {
                ShowError("The given mp3 file has a non mp3 extension.");
                return;
            }
            if (!imagePaths.All(path => supportedImageExtensions.Contains(Path.GetExtension(path).ToLower())))
            {
                ShowError("There are some images which has a non supported extension.");
                return;
            }
            if (!File.Exists(mp3Path) || !imagePaths.All(path => File.Exists(path)))
            {
                ShowError("Some files given to Mp3CoverDroper is not found, please check first.");
                return;
            }

            // get the mp3 file
            Mp3 mp3;

            try {
                mp3 = new Mp3(mp3Path, Mp3Permissions.ReadWrite);
            } catch (UnauthorizedAccessException ex) {
                var principal = new WindowsPrincipal(WindowsIdentity.GetCurrent());
                if (!principal.IsInRole(WindowsBuiltInRole.Administrator))
                {
                    // not in admin, try to get admin authority
                    ProcessStartInfo psi = new ProcessStartInfo {
                        FileName  = Application.ExecutablePath,
                        Arguments = string.Join(" ", args),
                        Verb      = "runas"
                    };
                    Process.Start(psi);
                    return;
                }
                ShowError($"You have no permission to write this mp3 file. Details:\n{ex}");
                return;
            } catch (Exception ex) {
                ShowError($"Failed to read mp3 file. Details:\n{ex}");
                return;
            }

            // process the mp3 file
            try {
                MainProcess(mp3, mp3Path, imagePaths);
            } catch (Exception ex) {
                ShowError($"Failed to execute the option. Details:\n{ex}");
            } finally {
                mp3.Dispose();
            }
        }
Ejemplo n.º 2
0
 void IDisposable.Dispose()
 {
     _mp3.Dispose();
 }