简体   繁体   English

从 C# 调用时无法在 Exiftool 中写入 CSV(输出到屏幕)

[英]Can't write CSV in Exiftool when called from C# (output goes to screen)

I have ac# process that uses exiftool to scan the contents of a directory and dump it into a csv file.我有 ac# 进程,它使用 exiftool 扫描目录的内容并将其转储到 csv 文件中。 While in 'real' code the aim will be that any output gets picked up by c#, for debugging I am explicitly starting a visible process so I can debug.虽然在“真实”代码中,目标是任何输出都被 c# 拾取,为了调试,我明确地启动了一个可见的进程,以便我可以调试。 In reality it has no relevance for the problem, regardless of the process window it's the same.实际上,它与问题无关,无论进程窗口如何,它都是相同的。

Process prc_exifTool = new Process();

prc_exifTool.StartInfo.UseShellExecute = false;

prc_exifTool.StartInfo.FileName = "exiftool.exe";
prc_exifTool.StartInfo.CreateNoWindow = false;
prc_exifTool.StartInfo.Arguments = args;
//tmpCSVFileName is a csv file whose name is the same as the folder that's being scanned + ".csv" 
File.Create(Path.Combine(Path.GetTempPath(), tmpCSVFileName)).Dispose(); // seems to be needed else the output complains that the file does not exist

prc_exifTool.Start();
prc_exifTool.WaitForExit();

...where ...在哪里

args =  -k -a -g -s -charset filename=utf8 -charset exif=utf8 -charset iptc=utf8 -ee -struct -progress -c "%.6f" -GPSAltitude -exif:GPSAltitude -ext jpg -csv -Directory "D:\temp" > "C:\Users\nemet\AppData\Local\Temp\temp.csv"

(in reality there's a fair few more tags and file formats and I won't ultimately be requiring to wait for ENTER to be pressed but for debug & the sake of keeping this comparatively short here, I'm not listing them as they're not relevant) (实际上,还有更多的标签和文件格式,我最终不需要等待 ENTER 被按下,但是为了调试和在这里保持相对较短,我没有列出它们,因为它们是不相干)

problem:问题:

When I use a cmd window and type in manually "exiftool -k -a -g .... (args)" [obvs substitute with args] the process runs okay and the csv gets filled with relevant data.当我使用 cmd 窗口并手动输入"exiftool -k -a -g .... (args)" [obvs 替换为 args] 时,该过程运行正常,并且 csv 充满了相关数据。 When I run the c# code the output instead of going into a csv file all goes onto the screen.当我运行 c# 代码时,输​​出而不是进入 csv 文件都进入屏幕。 What am I doing incorrectly?我做错了什么? One thing I've noticed that if I do cmd then the message is that "1 directories scanned", which is correct but when done in c# the msg says "2 directories scanned", so I think it's thinking the CSV is a folder, not a file.我注意到的一件事是,如果我执行 cmd,则消息是“扫描了 1 个目录”,这是正确的,但是在 c# 中完成时,味精说“扫描了 2 个目录”,所以我认为它认为 CSV 是一个文件夹,不是文件。

This link https://exiftool.org/forum/index.php?topic=7835.0 brings up a similar issue but there they're using OSX whereas I'm using Windows and the author doesn't really provide an answer apart from ...And I don't know if redirection (">") will work in the GUI.此链接https://exiftool.org/forum/index.php?topic=7835.0提出了类似的问题,但他们使用的是 OSX,而​​我使用的是 Windows,而作者除了. ..而且我不知道重定向(“>”)是否可以在 GUI 中工作。 -- and suggests reading the FAQ, which doesn't really mention much on this topic. - 并建议阅读常见问题解答,它并没有真正提及这个主题。

After some digging....the ">" is a redirect function of CMD, which doesn't really exist in that particular context that was in my question and so it does not work.经过一番挖掘......“>”是CMD的重定向功能,它在我的问题中的特定上下文中并不真正存在,因此它不起作用。 Instead CMD itself must be launched as a process.相反,CMD 本身必须作为一个进程启动。 The following logic works: [doubleQuote is just that, " <-- introduced as a string further up in the code]以下逻辑有效:[doubleQuote 就是这样,“ <-- 在代码中作为字符串引入]

Process prc_exifTool = new Process();
prc_exifTool.StartInfo.FileName = @"c:\windows\system32\cmd.exe";
prc_exifTool.StartInfo.Arguments = @"/k " + doubleQuote + "exiftool " + args  + doubleQuote + "&& exit"; // the "&& exit" is needed otherwise the cmd.exe never exits.

This creates the CSV file as required while maintaining the ability to parse the output for further processing.这将根据需要创建 CSV 文件,同时保持解析输出以进行进一步处理的能力。

Should anyone be interested, to parse the output "live" the whole code:如果有人有兴趣,解析输出“实时”整个代码:

Process prc_exifTool = new Process();
prc_exifTool.StartInfo.FileName = @"c:\windows\system32\cmd.exe";
prc_exifTool.StartInfo.Arguments = @"/k " + doubleQuote + "exiftool " + args  + doubleQuote + "&& exit";

prc_exifTool.StartInfo.CreateNoWindow = true;
prc_exifTool.StartInfo.UseShellExecute = false;
prc_exifTool.StartInfo.RedirectStandardOutput = true;
prc_exifTool.StartInfo.RedirectStandardError = true;
prc_exifTool.StartInfo.RedirectStandardInput = true;

prc_exifTool.StartInfo.StandardOutputEncoding = Encoding.UTF8;
prc_exifTool.StartInfo.StandardErrorEncoding = Encoding.UTF8;

prc_exifTool.OutputDataReceived += new DataReceivedEventHandler((s, e) =>
{
    Console.WriteLine(e.Data);
});
prc_exifTool.ErrorDataReceived += new DataReceivedEventHandler((s, e) =>
{
    Console.WriteLine(e.Data);
});

prc_exifTool.Start();
prc_exifTool.BeginOutputReadLine();
prc_exifTool.BeginErrorReadLine();
prc_exifTool.WaitForExit();
prc_exifTool.Close();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM