SysDirTest by eMeL


It's a very simple try-out program for test Windows special folders.

FREEWARE


Go back to SysDirTest page

Sample Source (BCB5):

//#include <shlobj.h>

SHSTDAPI_(BOOL) SHGetSpecialFolderPathA(HWND hwnd, LPSTR pszPath, int csidl, BOOL fCreate);

#define CSIDL_PERSONAL                  0x0005        // My Documents
#define CSIDL_TEMPLATES                 0x0015
#define CSIDL_APPDATA                   0x001a        // <user name>\Application Data
#define CSIDL_LOCAL_APPDATA             0x001c        // <user name>\Local Settings\Applicaiton Data (non roaming)
#define CSIDL_COMMON_APPDATA            0x0023        // All Users\Application Data
#define CSIDL_PROGRAM_FILES             0x0026        // C:\Program Files
#define CSIDL_PROGRAM_FILES_COMMON      0x002b        // C:\Program Files\Common
#define CSIDL_COMMON_TEMPLATES          0x002d        // All Users\Templates
#define CSIDL_COMMON_DOCUMENTS          0x002e        // All Users\Documents

#define CSIDL_WINDOWS                   0x0024        // GetWindowsDirectory()
#define CSIDL_SYSTEM                    0x0025        // GetSystemDirectory()

//---------------------------------------------------------------------------

AnsiString TfrmSysDirTest::TryDir(AnsiString sPath)
{
  char * sFileName =  "___eMeL_SysDirTest.tmp";

  AnsiString sFile = ExcludeTrailingBackslash(sPath) + "\\" + sFileName;

  TFileStream * fsTest = NULL;

  AnsiString sMsg;

  //

  if (iTryDir_Mode == tdmCreate)
  {
    try
    {
      fsTest = new TFileStream(sFile, fmCreate);
      sMsg = "testfile created";
    }
    catch(...)
    {
      sMsg = "testfile create ERROR!";
    }
  }
  else if (iTryDir_Mode == tdmErase)
  {
    if (! FileExists(sFile))
    {
      sMsg = "testfile not exists";
    }
    else if (DeleteFile(sFile))
    {
      sMsg = "testfile deleted";
    }
    else
    {
      sMsg = "testfile delete ERROR!";
    }
  }
  else
  { // iTryDir_Mode == tdmTest
    if (! FileExists(sFile))
    {
      try
      {
        fsTest = new TFileStream(sFile, fmCreate);
      }
      catch(...)
      {
        sMsg = "  Create:No";
      }

      delete fsTest;
      fsTest = NULL;
    }

    //

    if (FileExists(sFile))
    {
      try
      {
        fsTest = new TFileStream(sFile, fmOpenRead);
        sMsg  += "  RO:Yes";
      }
      catch(...)
      {
        sMsg += "  RO:No";
      }

      delete fsTest;
      fsTest = NULL;
    }

    //

    try
    {
      fsTest = new TFileStream(sFile, fmOpenReadWrite);
      sMsg  += "  RW:Yes";
    }
    catch(...)
    {
      sMsg += "  RW:No";
    }
  }

  delete fsTest;

  //

  return sPath + "  [" + sMsg.Trim() + "]";
}
//---------------------------------------------------------------------------

void __fastcall TfrmSysDirTest::btnTestClick(TObject *Sender)
{
  if (Sender == btnCreateTestFiles)
  {
    iTryDir_Mode = tdmCreate;
  }
  else if (Sender == btnEraseTestFiles)
  {
    iTryDir_Mode = tdmErase;
  }
  else
  {
    iTryDir_Mode = tdmTest;
  }


  lbDisp->Clear();

  AnsiString sMsg;
  
  TCHAR szPath[MAX_PATH + 64];        // more for safe

  //------

  sMsg = "CSIDL_PROGRAM_FILES :        ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_PROGRAM_FILES, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------

  sMsg = "CSIDL_PROGRAM_FILES_COMMON : ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_PROGRAM_FILES_COMMON, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------

  lbDisp->Items->Add("");


  sMsg = "CSIDL_APPDATA :              ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_APPDATA, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------

  sMsg = "CSIDL_LOCAL_APPDATA :        ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_LOCAL_APPDATA, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------

  sMsg = "CSIDL_PERSONAL :             ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_PERSONAL, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------

  sMsg = "CSIDL_TEMPLATES :            ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_TEMPLATES, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------

  lbDisp->Items->Add("");


  sMsg = "CSIDL_COMMON_APPDATA :       ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_COMMON_APPDATA, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------
  
  sMsg = "CSIDL_COMMON_DOCUMENTS :     ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_COMMON_DOCUMENTS, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------

  sMsg = "CSIDL_COMMON_TEMPLATES :     ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_COMMON_TEMPLATES, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------

  lbDisp->Items->Add("");


  sMsg = "CSIDL_WINDOWS :              ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_WINDOWS, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }

  //------

  sMsg = "CSIDL_SYSTEM :               ";

  if (SHGetSpecialFolderPathA(Handle, szPath, CSIDL_SYSTEM, true))
  {
    lbDisp->Items->Add(sMsg + TryDir(szPath));
  }
  else
  {
    lbDisp->Items->Add(sMsg + "return FALSE!");
  }
}
//---------------------------------------------------------------------------





Download Source

Download EXE



www.emel.hu/freeware