Okay this is weird behaviour, I'll try to describe it well: I've written a very short C# program which sends the names of all *.png files on my Desktop to notepad. First I start notepad.exe, check the PID in TaskManager and enter the PID into my C# program (so that AppActivate knows, which PID it should focus before sending the keys). Then I compile myprogram.cs via csc.exe and execute it.
Here's the C# program:
using System;
using System.Reflection;
using System.IO;
namespace MyApp {
class MyClass {
static void Main(string[] args) {
Type type = Type.GetTypeFromProgID("WScript.Shell"); // Get a WScript.Shell object
object inst = Activator.CreateInstance(type);
object catchReturn;
string[] myfiles = Directory.GetFiles(@"U:\Desktop", "*.png"); // Get all .png files on the Desktop
foreach (string myfile in myfiles){
catchReturn = (object)type.InvokeMember("AppActivate", BindingFlags.InvokeMethod, null, inst, new object[]{"7080"}); // 7080 is the PID that I looked up in Windows TaskManager
// When I send the keys to the console instead of notepad, I comment out the line above
catchReturn = (object)type.InvokeMember("SendKeys", BindingFlags.InvokeMethod, null, inst, new object[]{myfile + " ", true}); // I send the filename <myfile> and set SendKeys' second parameter to true
}
}
}
}
This is what the program prints to notepad:
U:Destop\wof.png U:\Desktop\Bild1.png U:\Desktop\leipzig_koeln_freitag.png U:\Desktop\leipzig_koeln_sonntag.png U:\Desktop\npp.png U:\Desktop\eztwain.png
The first png is actually wolf.png, not wof.png. And obviously the location is U:\Desktop, not U:Destop. Letters get randomly swallowed. If I execute the program again, other letters will get swallowed.
However, if I omit AppActivate (meaning, I print the .png names to the console which is calling myprogram.exe), not a single letter gets swallowed!
I wonder especially why letters get swallowed at random positions instead of e.g. only the first x characters?