1

I use OPENFILENAME and the function GetOpenFileName() to get a file location through the windows file browser.

The problem is when I want to delete the chosen folder (when the program running and I need to do this) windows show an error: "The action can't be completed because the folder or a file in it is open in another program"

I know why it does that but I don't know how to close this file during runtime

Thanks, in advance.

EDIT :

//Opening Save file
    TCHAR *filter = _T("Story File(*.Strory*)\0*.Story*\0");
    HWND owner = NULL;

    OPENFILENAME ofn;
    TCHAR fileName[MAX_PATH] = _T("");
    ZeroMemory(&ofn, sizeof(ofn));
    ofn.lStructSize = sizeof(OPENFILENAME);
    ofn.hwndOwner = owner;
    ofn.lpstrFilter = filter;
    ofn.lpstrFile = fileName;
    ofn.nMaxFile = MAX_PATH;
    ofn.Flags = OFN_EXPLORER | OFN_FILEMUSTEXIST | OFN_HIDEREADONLY;
    ofn.lpstrDefExt = _T("");

    if (GetOpenFileName(&ofn))
    {

    }

This code is where I create and use the OPENFILENAME variable, the GetOpenFileName() will automatically lock the chosen file as "open in a program" and prevent any modification of the folder during the runtime of the program (like delete or rename). But I want to disable this property.

Asesh
  • 3,186
  • 2
  • 21
  • 31
Carl
  • 47
  • 7
  • Would you be able to share some more details on what you are trying to do, as well as the code snippet you are having problems with? – Matt Jan 09 '18 at 10:41
  • I've made an edit if you want @Matt – Carl Jan 09 '18 at 10:52
  • _"...`GetOpenFileName()` will automatically lock the chosen file as "open in a program"..."_: no it does not see the MS example code: https://msdn.microsoft.com/en-us/library/windows/desktop/ms646829(v=vs.85).aspx#open_file You have to call `CreateFile` when `GetOpenFileName` returns to open the file. – Richard Critten Jan 09 '18 at 10:55
  • if you not use `OFN_NOCHANGEDIR` flag the `GetOpenFileName` open handle to folder, where you select file. exactly this *folder* handle (cuurent directory) prevent for delete – RbMm Jan 09 '18 at 11:26
  • @RichardCritten: already tested it before and it doesn't change anything (I test it a second time after your answer and it doesn't solve my problem too). – Carl Jan 09 '18 at 12:14
  • @RbMm: Thanks it works but I've another problem, I can delete from the browser but I can't delete the folder with my program maybe you know why? – Carl Jan 09 '18 at 12:27
  • *but I can't delete the folder with my program maybe you know why* - without view your code - no. but that some another code can delete folder - say that this is possible – RbMm Jan 09 '18 at 13:12

2 Answers2

5

if you not use OFN_NOCHANGEDIR flag in OPENFILENAME the GetOpenFileName open handle for directory, where you select file and set it as current directory. exactly this folder handle prevent from delete it. you can use OFN_NOCHANGEDIR flag or before delete folder change current directory for some another. say to windows directory - you not delete it:

WCHAR path[MAX_PATH];
GetSystemWindowsDirectoryW(path, RTL_NUMBER_OF(path));
SetCurrentDirectoryW(path);
RbMm
  • 31,280
  • 3
  • 35
  • 56
  • I find the solution to my comment: "@RbMm: Thanks it works but I've another problem, I can delete from the browser but I can't delete the folder with my program maybe you know why", this part is my mistake thanks for your answer. – Carl Jan 09 '18 at 12:41
  • Save my life, thanks. – l2m2 Dec 02 '21 at 02:28
0

Some other application owns an open handle for the folder which you want to delete. Windows is not able to delete the folder if there are any other clients dealting with data stored in the folder. To solve this do you can try to:

  • Check if any open explorer windows are ther which are showing the folder / any containing file from this folder you want to delete
  • Check if any command-prompts are open and their current directory is set to the folder you want to delete
  • Check if your application ( or any other ) are using any data from this folder ( like a notepad which has opened a textfile from this folder for instance )
KimKulling
  • 2,654
  • 1
  • 15
  • 26
  • There is no other app or anything open on this folder, and I only can't delete the folder during the runtime of my program. When I close it (my program), I able to delete this it comes from my program. – Carl Jan 09 '18 at 10:51
  • Are there any file-descriptors opened for the folder by your own app? – KimKulling Jan 09 '18 at 12:49