Current Directory
by
, 30-Oct-2011 at 06:46 AM (7748 Views)
A couple of weeks ago in a Discovering Visual DataFlex training one of the trainees stumbled over a problem caused by the Current Directory. In this blog I tell you more about the problem and what the Current Directory has to do with it.
The Problem
In the case that let me make this blog a dbBitmap control was used to display an image. A dbBitmap control is a database aware control so the name of the image file is stored in a column of a row in a table. Pictures can be in a folder of the work-space or anywhere else in your environment (own hard-disk or network). If there are not in your work-space you can decide to use the Store_With_Pathname_State property or not. When this property is set to true the path to the image is stored but in multi-user environments this easily causes a problem as the image location might not be reachable for all users. Therefore it is better to not store the path and have the image in one of the paths of your work-space.
The default for the Store_With_Pathname_State property is false which means that the picture needs to be in one of paths of the work-space. For the image (bitmap) file selection we used a Windows Common File Dialog object (class defined in File_Dlg.Pkg) and we decided that after the file selection we wanted to find out if the chosen image file was present / addressable via the paths of the current work-space or not. If the image could not be found we wanted to copy it over from the source location to one of the folders in the bitmap path of the work-space. This way the image is accessible via a relative path. The code written for this used the Get_File_Path command. This command searches the DataFlex search paths to see if the given file name can be located or not.
The Path
What is this DataFlex search path? The path - named DFPATH (DataFlex Path) - was introduced in the product in the middle of the 80's and it is used to find files needed by the application. Files such as the embedded database tables, foreign database intermediate files (.INT), sequential I/O files (direct_input/output), license files, help files etc. In your application the current path can be retrieved via the DF_OPEN_PATH attribute.
At the start of your application the path is the same as the DFPATH setting stored in the Windows Registry (for Visual DataFlex 16.1 on a Windows 7 64 bit OS look for it at HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Data Access Worldwide\Visual DataFlex\16.1\Defaults). On my computer this is:
As soon as the cApplication object is created the path is changed and contains the folders of the current workspace. Again on my machine:Code:.\;C:\ProgramData\Data Access Worldwide\Visual DataFlex\16.1\License Files\;C:\Appl\VDF\Studio\16.1\Bin\;C:\Appl\VDF\Studio\16.1\Usr\;C:\Appl\VDF\Studio\16.1\Lib\;C:\Appl\VDF\Studio\16.1\Bitmaps\;C:\Appl\VDF\Studio\16.1\Help\
The Get_File_Path command searches the path listed above to see if a file with the name passed to the command can be found.Code:C:\Appl\VDF\Examples\16.1\Order Entry\Data\;C:\Appl\VDF\Examples\16.1\Order Entry\Bitmaps\;C:\Appl\VDF\Examples\16.1\Order Entry\Help\;C:\Appl\VDF\Examples\16.1\Order Entry\Programs\;.\;C:\ProgramData\Data Access Worldwide\Visual DataFlex\16.1\License Files\;C:\Appl\VDF\Studio\16.1\Bin\;C:\Appl\VDF\Studio\16.1\Usr\;C:\Appl\VDF\Studio\16.1\Lib\;C:\Appl\VDF\Studio\16.1\Bitmaps\;C:\Appl\VDF\Studio\16.1\Help\
If no file can be found with this name stored in sFileName the sFileNameWithPath will be empty.Code:Get_File_Path sFileName to sFileNameWithPath
Current Directory
What has this all to do with the Current Directory? Look good at the default DFPATH and notice that it contains a "." which stands for the Current Directory. In your application you can retrieve the Current Directory with the command Get_Current_Directory. At the start of your application it will be set to the folder of the executable or the path in the "Start in" attribute of the shortcut to your executable or the Windows. If you start your application from the Visual DataFlex Studio it is most likely the Windows system32 as the short-cut to the Visual DataFlex Studio does not have a value specified for "Start in". If you specify a value (it has to be a valid folder name) in the "Start in" attribute that folder will be the Current Directory.
As long as the current directory points to a folder in your work-space there is no problem with the '.' in the search path as the file we want to locate is either in that folder or not and thus not relative to your work-space. If the current directory points to a folder anywhere else on your computer the Get_File_Path might find the image while image is not in your work-space. And here is where the problem lies. An object of the OpenDialog class changes the current directory if you browse to a folder or if you specify a folder in the Initial_Folder property. This means that if you select an image with the (built-in) image selection via an OpenDialog object and then test if the image exists in your path with the Get_File_Path that it tests as successful.
The change of the current directory can be avoided by setting the NoChangeDir_State of the OpenDialog to false. File selection will still work and the Get_File_Path does no longer return a valid path if the file is not local to the work-space or in the current directory.
Don't use the Set_Directory command as this changes the current directory as well.
Conclusion
While the current directory can be very useful it might also cause problems with your application and I won't advise you to trust on the path.