Search code examples
c#fileparsingpowerpoint

Parse the file fullpath and name into c# console app


I try to implement an app which would allow me to convert a PPT or PPTX files into jpgs and I plan to use it globally on my pc. The problem I am facing is that I don't know how to parse the file I open in the app. Basically I need the app to get the fullpath and filename when I open a specific file so hardcoding some specific route is out of question.

General idea: I take a ppt file and drag it onto the .exe file then it processes the ppt file and in the end i have a folder with all the jpgs in the same place as the ppt. This is what I have so far:

using Microsoft.Office.Core;
using System;
using PowerPoint = Microsoft.Office.Interop.PowerPoint;

namespace PPInterop
{
    class Program
    {
        static void Main(string[] args)
        {
            

            var app = new PowerPoint.Application();

            var pres = app.Presentations;

            var file = pres.Open(@"C:\presentation1.jpg", MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);

            file.SaveCopyAs(@"C:\presentation1.jpg", PowerPoint.PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoTrue);

            Console.ReadKey();
        }
    }
}

Solution

  • Concerning the parsing I think you've already found Microsoft's way to do this by using code from Microsoft.Office.Interop.PowerPoint.

    But you're loading a JPG instead of a PPT or PPTX file. Drag & drop should also be possible by using args as (at least Windows) will run the application with the dragged file as argument (see here).

    So I think could be something like this:

    static void Main(string[] args)
    {
        if (args.Length == 0)
            throw new Exception("No presentation file given");
    
        var presentationFilePath = new FileInfo(args[0]);
    
        if (presentationFilePath.Exists == false)
            throw new FileNotFoundException("No presentation given");
    
        var app = new PowerPoint.Application();
        var presentation = app.Presentations.Open(presentationFilePath, MsoTriState.msoTrue, MsoTriState.msoTrue, MsoTriState.msoFalse);
        var jpgName = Path.GetFileNameWithoutExtension(presentationFilePath.FullName) + ".jpg";
        presentation.SaveCopyAs(jpgName, PowerPoint.PpSaveAsFileType.ppSaveAsJPG, MsoTriState.msoTrue);
    
        Console.WriteLine("Converted " + presentationFilePath + " to " + jpgName);
    }