j2associates
j2associates

Reputation: 1155

How can I use the Windows SpecialFolder enumeration in Dynamics NAV

I want to access the Windows SpecialFolder enum from within C/AL code. What is the best way to do this?

Upvotes: 0

Views: 104

Answers (1)

j2associates
j2associates

Reputation: 1155

After some research and testing, I came up with the following methods. They could go in a codeunit or codeunit File Management (419). I have also included a TestSpecialFolders method at the end.

1. GetSpecialFolder takes a SpecialFolder enum value and returns the associated path
2. GetSpecialFolders returns all of the names and values in a temp Name/Value Buffer table (823)
   a. LoadSpecialFolder is a helper function to accomplish the task

3. GetSpecialFolderNames returns a comma delimited list of SpecialFolder enum Names
4. GetSpecialFolderValues returns a comma and newline delimited list of SpecialFolder enum Values
5. GetSpecialFolderNamesAndValues returns a list of SpecialFolder enum Names and Values

6. TestSpecialFolders

Note that DotNet enumeration values have a . separator instead of a :: separator.

Note also that you can sometimes, but not always, make DotNet variables version agnostic by removing the Version, Culture and PublicKeyToken properties.

For example, System.Drawing.Bitmap.'System.Drawing' instead of the fully qualified System.Drawing.Bitmap.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'

Enjoy...

PROCEDURE GetSpecialFolder@1000000008("Special Folder"@1000000000 : DotNet "'mscorlib'.System.Environment+SpecialFolder") : Text;
VAR
  Environment@1000000001 : DotNet "'mscorlib'.System.Environment";
BEGIN
  EXIT(Environment.GetFolderPath("Special Folder"));
END;

PROCEDURE GetSpecialFolders@1000000003(VAR "Special Folders List"@1000000000 : TEMPORARY Record 823);
VAR
  ID@1000000002 : Integer;
  Counter@1000000001 : Integer;
  SpecialFolders@1000000003 : DotNet "'mscorlib'.System.Environment+SpecialFolder";
BEGIN
  CLEAR("Special Folders List");
  "Special Folders List".RESET;
  "Special Folders List".DELETEALL;
  "Special Folders List".SETCURRENTKEY(ID);

  // It would be nice to be able to iterate these.
  LoadSpecialFolder(SpecialFolders.Desktop, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.Programs, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.Personal, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.MyDocuments, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.Favorites, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.Startup, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.Recent, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.SendTo, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.StartMenu, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.MyMusic, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.DesktopDirectory, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.MyComputer, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.Templates, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.ApplicationData, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.LocalApplicationData, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.InternetCache, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.Cookies, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.History, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.CommonApplicationData, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.System, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.ProgramFiles, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.MyPictures, ID, "Special Folders List");
  LoadSpecialFolder(SpecialFolders.CommonProgramFiles, ID, "Special Folders List");

  Counter := "Special Folders List".COUNT;
END;

PROCEDURE GetSpecialFolderNames@1000000007(VAR "Special Folders List"@1000000000 : TEMPORARY Record 823;VAR Names@1000000002 : Text);
VAR
  Counter@1000000001 : Integer;
  Delimiter@1000000003 : Text;
BEGIN
  Names := '';
  WITH "Special Folders List" DO
  BEGIN
    RESET;
    SETCURRENTKEY(ID);
    Counter := COUNT;
    IF FINDFIRST THEN
    BEGIN
      REPEAT
        Names += Delimiter + Name;
        Delimiter := ', ';
      UNTIL NEXT = 0;
    END;
  END;
END;

PROCEDURE GetSpecialFolderValues@1000000012(VAR "Special Folders List"@1000000000 : TEMPORARY Record 823;VAR Values@1000000002 : Text);
VAR
  Counter@1000000001 : Integer;
  Delimiter@1000000003 : Text;
BEGIN
  Values := '';
  WITH "Special Folders List" DO
  BEGIN
    RESET;
    SETCURRENTKEY(ID);
    Counter := COUNT;
    IF FINDFIRST THEN
    BEGIN
      REPEAT
        Values += Delimiter + Value;
        Delimiter := ', \';
      UNTIL NEXT = 0;
    END;
  END;
END;

PROCEDURE GetSpecialFolderNamesAndValues@1000000015(VAR "Special Folders List"@1000000000 : TEMPORARY Record 823;VAR Names@1000000002 : Text;VAR Values@1000000005 : Text);
VAR
  Counter@1000000001 : Integer;
  Delimiter@1000000003 : Text;
BEGIN
  GetSpecialFolderNames("Special Folders List", Names);
  GetSpecialFolderValues("Special Folders List", Values);
END;

LOCAL PROCEDURE LoadSpecialFolder@1000000036(SpecialFolders@1000000004 : DotNet "'mscorlib'.System.Environment+SpecialFolder";VAR ID@1000000002 : Integer;VAR "Special Folders List"@1000000001 : TEMPORARY Record 823);
VAR
  Environment@1000000000 : DotNet "'mscorlib'.System.Environment";
  EnumName@1000000003 : Text;
  EnumValue@1000000005 : Text;
BEGIN
  ID += 1;
  EnumName := FORMAT(SpecialFolders);
  EnumValue := Environment.GetFolderPath(SpecialFolders);

  "Special Folders List".INIT;
  "Special Folders List".ID := ID;
  "Special Folders List".Name := EnumName;
  "Special Folders List".Value := EnumValue;
  "Special Folders List".INSERT;
END;

LOCAL PROCEDURE TestSpecialFolders@1000000014();
VAR
  SpecialFoldersList@1000000003 : TEMPORARY Record 823;
  FileManagement@1000000002 : Codeunit 419;
  SpecialFolder@1000000004 : DotNet "'mscorlib'.System.Environment+SpecialFolder";
  Names@1000000005 : Text;
  Values@1000000006 : Text;
BEGIN
  SpecialFolder := SpecialFolder.Desktop;
  MESSAGE('%1\    %2', FORMAT(SpecialFolder), FileManagement.GetSpecialFolder(SpecialFolder));
  SpecialFolder := SpecialFolder.Favorites;
  MESSAGE('%1\    %2', FORMAT(SpecialFolder), FileManagement.GetSpecialFolder(SpecialFolder));

  FileManagement.GetSpecialFolders(SpecialFoldersList);

  FileManagement.GetSpecialFolderNames(SpecialFoldersList, Names);
  MESSAGE('Special Folder Names:\\''%1''.', Names);

  FileManagement.GetSpecialFolderValues(SpecialFoldersList, Values);
  MESSAGE('Special Folder Values:\\''%1''.', Values);

  FileManagement.GetSpecialFolderNamesAndValues(SpecialFoldersList, Names, Values);
  MESSAGE('Special Folder Names:\\''%1''\\Special Folder Values:\\''%2''.', Names, Values);

  MESSAGE('Completed');
END;

Upvotes: 0

Related Questions