Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Check Files in used

Check Files in used

Scheduled Pinned Locked Moved C / C++ / MFC
c++tutorial
4 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    thangnvhl
    wrote on last edited by
    #1

    I am writing a program that will delete file(s) in a specified folder except file(s) in used. Please show me how to check whether a file is using or not. (Use C/C++ Non-MFC) Thanks a lot

    Sincerely Thangnvhl

    A R 2 Replies Last reply
    0
    • T thangnvhl

      I am writing a program that will delete file(s) in a specified folder except file(s) in used. Please show me how to check whether a file is using or not. (Use C/C++ Non-MFC) Thanks a lot

      Sincerely Thangnvhl

      A Offline
      A Offline
      aldo hexosa
      wrote on last edited by
      #2

      may be: HANDLE hFile = CreateFile("test.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,NULL); if(hFile == INVALID_HANDLE_VALUE){ if(GetLastError() == 5 || GetLastError() == 4) { //this file is being used } }else { CloseHandle(hFile); }

      D 1 Reply Last reply
      0
      • T thangnvhl

        I am writing a program that will delete file(s) in a specified folder except file(s) in used. Please show me how to check whether a file is using or not. (Use C/C++ Non-MFC) Thanks a lot

        Sincerely Thangnvhl

        R Offline
        R Offline
        Renjith Ramachandran
        wrote on last edited by
        #3

        an alternative way i think is to use the OF_SHARE_EXCLUSIVE flag as the third argument for the OpenFile() function.

        May all beings be happy and free...

        1 Reply Last reply
        0
        • A aldo hexosa

          may be: HANDLE hFile = CreateFile("test.txt",GENERIC_WRITE,FILE_SHARE_READ,NULL,OPEN_EXISTING,FILE_ATTRIBUTE_NORMAL ,NULL); if(hFile == INVALID_HANDLE_VALUE){ if(GetLastError() == 5 || GetLastError() == 4) { //this file is being used } }else { CloseHandle(hFile); }

          D Offline
          D Offline
          David Crow
          wrote on last edited by
          #4

          aldo hexosa wrote:

          if(GetLastError() == 5 || GetLastError() == 4)

          Why use constants? It would be much more readable to use:

          if (GetLastError() == ERROR_TOO_MANY_OPEN_FILES || GetLastError() == ERROR_ACCESS_DENIED)


          "Talent without discipline is like an octopus on roller skates. There's plenty of movement, but you never know if it's going to be forward, backwards, or sideways." - H. Jackson Brown, Jr.

          "Judge not by the eye but by the heart." - Native American Proverb

          1 Reply Last reply
          0
          Reply
          • Reply as topic
          Log in to reply
          • Oldest to Newest
          • Newest to Oldest
          • Most Votes


          • Login

          • Don't have an account? Register

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • World
          • Users
          • Groups