我想从 C/AL 代码中访问 Windows SpecialFolder 枚举。做这个的最好方式是什么?
问问题
58 次
1 回答
0
经过一番研究和测试,我想出了以下方法。他们可以进入代码单元或代码单元文件管理(419)。最后我还包含了一个 TestSpecialFolders 方法。
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
请注意,DotNet 枚举值具有 . 分隔符而不是 :: 分隔符。
另请注意,您有时(但并非总是)可以通过删除 Version、Culture 和 PublicKeyToken 属性使 DotNet 变量版本不可知。
例如,System.Drawing.Bitmap.'System.Drawing' 而不是完全限定的 System.Drawing.Bitmap.'System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'
享受...
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;
于 2020-03-03T16:03:24.503 回答