1

Again I find myself struggling with C# after coming from a limited vb.net knowledge base.

I am attempting to press a button on my application that will open a file dialogue and open the selected files in photoshop and run an action on them.

In VB.net all I had to do was the following...

 Private launchFiles As OpenFileDialog
Private Sub OpenInPS_btn_Click(sender As Object, e As EventArgs) Handles OpenInPS_btn.Click

    launchFiles = New OpenFileDialog
    launchFiles.Title = "Select files"
    launchFiles.CheckFileExists = True
    launchFiles.Multiselect = True
    launchFiles.RestoreDirectory = False
    Dim appRef
    appRef = CreateObject("Photoshop.Application")

    If launchFiles.ShowDialog = Windows.Forms.DialogResult.OK Then
        For Each fl In launchFiles.FileNames
            appRef.Open(fl)
            appRef.DoAction("JDE Brandstore", "Render to Brandstore V3")
    End If

End Sub

However, when I attempt to convert this to C# like so...

 private OpenFileDialog launchFiles;


    private void BTN_Photoshop_Click(object sender, EventArgs e)
    {
        launchFiles = new OpenFileDialog();
        launchFiles.Title = "Select files";
        launchFiles.CheckFileExists = true;
        launchFiles.Multiselect = true;
        launchFiles.RestoreDirectory = false;
        object appRef;
        appRef = CreateObject("Photoshop.Application");
        if (launchFiles.ShowDialog == Forms.DialogResult.OK)
        {
            foreach (var fl in launchFiles.FileNames)
            {
                appRef.Open(fl);
                appRef.DoAction("Job1", "CropperSpec");
            }
        }
    }

I got a whole host of errors that I don't currently understand/know how to resolve. enter image description here

Would anyone happen to know where I am going wrong and perhaps (here comes the cheeky request...) provide a working code block for me with explanation as to where I have gone wrong and what your code is doing to make it work.

---Update---

Thanks for the Assist @Mat J

New version of code here now launches Photoshop but I cannot get it to open any documents.

 private OpenFileDialog launchFiles;


    private void BTN_Photoshop_Click(object sender, EventArgs e)
    {
        launchFiles = new OpenFileDialog();
        launchFiles.Title = "Select files";
        launchFiles.CheckFileExists = true;
        launchFiles.Multiselect = true;
        launchFiles.RestoreDirectory = false;

        Type PhotoshopType = Type.GetTypeFromProgID("Photoshop.Application");
        object PhotoshopInst = Activator.CreateInstance(PhotoshopType);
        PhotoshopType.InvokeMember("Visible", BindingFlags.SetProperty, null, PhotoshopInst, new object[1] { true });
        if (launchFiles.ShowDialog() == DialogResult.OK)
        {
            foreach (var fl in launchFiles.FileNames)
            {
                PhotoshopType.open(fl);
            }
        }
    }

enter image description here Massive thanks

  • 1
    [This question](https://stackoverflow.com/q/13719579/219933) will get you started as to what to search for and how to approach it. The gist is there are things that are available only in one language and sometimes you cannot directly translate it to other language easily. – Mat J Jun 02 '21 at 06:10
  • Thanks for this Mat, Got the function running now. just need to know how to access the commands in PS foreach (var fl in launchFiles.FileNames) { appRef.Open(fl); appRef.DoAction("Job1", "CropperSpec"); } Are erroring as 'Object does not contain a Definition' – Oogle_doogleson Jun 02 '21 at 06:26
  • Can you update your code in the question? Like the question linked by @MatJ states, you have to either declare your `appRef` as `dynamic` instead of `object` or use [Type.InvokeMember](https://learn.microsoft.com/en-us/dotnet/api/system.type.invokemember?view=net-5.0) to call the methods `Open` and `DoAction`. – thabs Jun 02 '21 at 07:09
  • Code updated in original post – Oogle_doogleson Jun 02 '21 at 08:03

1 Answers1

2

Provided you use .NET 4 or later, you can declare PhotoshopInst as dynamic:

dynamic PhotoshopInst = Activator.CreateInstance(PhotoshopType);

Then you will be able to call methods you know the underlying type supports without the compiler complaining.

PhotoshopInst.Open(fl);
thabs
  • 603
  • 2
  • 13