1

New to windows forms/window handle.

Trying to save files in different locations.

SaveFileDialog Image:

enter image description here

I am able to get the edit box handle of the file name edit boxes in save file dialog. Able to paste the path.

enter image description here

private const int WM_SETTEXT = 0x000C;

IntPtr edithWnd = IntPtr.Zero;
edithWnd = FindWindowEx(edithWnd, IntPtr.Zero, "Edit", null);
SendMessage(edithWnd, WM_SETTEXT, IntPtr.Zero, "D:\Mine\Folder1\file");

Above code sets file path in file name text box.

Now, Clicking the "Save" button by getting its handle and sending a click.

private const int BM_CLICK = 0x00F5;

IntPtr handle = GetForegroundWindow(); // Save As dialog
IntPtr edithWnd = FindWindowEx(handle, IntPtr.Zero, "Button", "&Save");           
SendMessage(edithWnd, BM_CLICK, IntPtr.Zero, null);

It's working fine, but while using this code in the loop for multiple files to save in different locations, it's not working properly, it is saving all the files in one location only

For eg, file is saved in "D:\Mine\Folder1\file"

file1 is not being saved in "D:\Mine\Folder2\file1" instead it is being saved in "D:\Mine\Folder1\file1"

file2 is not being saved in "D:\Mine\Folder3\file2" instead it is being saved in "D:\Mine\Folder1\file2"

Seems like it is pointing to the first location only, no matter what the path is.

mkareshky
  • 202
  • 3
  • 7
Baj B
  • 65
  • 4
  • 1
    you seem to be showing parts of your code that work as expected, and then go on describing a part with a loop that does not work -> include that code. is the application that hosts this "save" dialog under your control, or are you automating another application? – Cee McSharpface Aug 24 '20 at 09:18
  • I have files in one of the locations.....I am programmatically opening those files and saving them again in different locations and closing the file, So looping all the available files in that location .....I just need the reason why this is not working or anything more that I need to do to have it working programmatically because while doing it manually, it is saving the files in desired folders – Baj B Aug 24 '20 at 09:28
  • related: https://stackoverflow.com/q/34735332/1132334 frame challenge: use a different approach to relocate your files. this may break anyway with newer versions of windows. – Cee McSharpface Aug 24 '20 at 09:40
  • `SaveFileDialog` has properties like `FileName` that allow you to set these values, why are you trying to use native Windows APIs instead? – Ian Kemp Aug 24 '20 at 10:26
  • lan Kemp, I think SaveFileDialog will be opened by clicking on menu item "Save As" , as per OP's comment, So we can not set it before, right ? because we are not creating a SaveFileDialog instance here, The SaveFileDialog will be opened on click, and OP is accessing it through window handle – Jay Aug 24 '20 at 12:24

1 Answers1

1

Why not to get into the desired path first and then save the file.

Worked for me..

SendMessage(edithWnd, WM_SETTEXT, IntPtr.Zero, "D:\Mine\Folder1\");
// Hit enter
SendKeys.SendWait("{ENTER}");
// Now click the save button, file name will be there already, in case if it is 
// not paste the file name first and then perform save button click

You might need to focus the file path before pressing "Enter" that will set the focus and pressing "Enter" will let you inside your folder path.

Dharman
  • 30,962
  • 25
  • 85
  • 135
Jay
  • 165
  • 2
  • 14
  • Yeah, this approach is useful in my case....I am Able to get into the folder first and then saving the file.....Thanks a lot – Baj B Aug 25 '20 at 14:11