C#: Problem with filepath in Commandline

Started by Brainbow, July 24, 2024, 04:23:24 PM

Previous topic - Next topic

Brainbow

Hello,

I am writing a simple application in C# to remove ratings of my images using SharpExifTool.

string exifToolArguments ="-rating= -ratingpercent= C:\\Users\\Anon\\Desktop\\TestFolder\\test.png"

using (var exiftool = new SharpExifTool.ExifTool())
{
  await exiftool.ExecuteAsync(exifToolArguments);
}

This Codeexample is working, but if my targetpath has empty spaces in its full pathname, the path can not be recognized by the program.
The path also doesn't like quotation marks at all. If I use \"C:\\Users\\Anon\\Desktop\\TestFolder\\test.png\", even the path without spaces won't work.

How can I fix that?
Thanks in advance

Phil Harvey

I'm guessing you should escape the quotes with a backslash.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Brainbow

Thanks for your reply.

You mean like this?
string exifToolArguments ="-rating= -ratingpercent= \"C:\\Users\\Anon\\Desktop\\TestFolder\\test.png\"";

I've already tried, doesn't even work on a path without spaces like this.

Phil Harvey

Oh well.  You should ask the SharpExifTool people how to pass arguments with special characters.

- Phil
...where DIR is the name of a directory/folder containing the images.  On Mac/Linux/PowerShell, use single quotes (') instead of double quotes (") around arguments containing a dollar sign ($).

Brainbow

You're right.

When i use your external Tool exiftool.exe and start it with my C# Code, it works like a charm.

string exifToolArguments = "-overwrite_original -rating= -ratingpercent= \"C:\\Users\\Anon\\Desktop\\TestFolder\\test.png\"";

var process = new Process
{
    StartInfo = new ProcessStartInfo
    {
        FileName = @"C:\Users\Anon\Downloads\Compressed\exiftool\exiftool.exe",
        Arguments = exifToolArguments,
        RedirectStandardOutput = true,
        RedirectStandardError = true,
        UseShellExecute = false,
        CreateNoWindow = true
    }
};

process.Start();

Even though the NuGet Package SharpExifTool is based on your Tool, the problem lies there.
Just would have been nice to have my program not rely on an external tool to work.  :P