question

Stephane R avatar image
0 Likes"
Stephane R asked Stephane R commented

How can I access the Windows logged in user name ?

I'm trying to access the Windows user name from inside FlexScript.

Either outputs of 'echo %username%' or 'whoami' would work for me.

I've tried to use runprogram("whoami") ; which returned a success code; but how do I retrieve the output of the command ?

Is there a simpler method ? Would the model already have a node with that info somewhere ?

FlexSim 19.0.6
windows 10usernamewhoami
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

1 Answer

·
Braydn T avatar image
2 Likes"
Braydn T answered Stephane R commented

@Stephane R The Windows command "whoami" just prints the current user to the terminal. The Flexscript command runprogram() just runs whatever program you specify. To get it into FlexSim you will have to create a .exe that you can run that will run "whoami", copy the output, and then import it into FlexSim.

Alternatively you could use the command userdatadir(), which returns the current user's roaming directory. The user name is displayed in that path.

· 2
5 |100000

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.

Phil BoBo avatar image Phil BoBo ♦♦ commented ·

Here's a script that will parse the user name out of userdatadir():

string dir = userdatadir();
int startIndex = dir.indexOf("Users") + 6;
string trimDir = dir.substr(startIndex, dir.length - startIndex);
int endIndex = trimDir.indexOf("\\") - 1;
string username = trimDir.substr(1, endIndex);
return username;

Alternatively, you could use the C++ GetUserName() function to get the user name. You would need to use the DLL Maker to call C++ code though. I'm not aware of a good way to get the user name from FlexScript.

0 Likes 0 ·
Stephane R avatar image Stephane R Phil BoBo ♦♦ commented ·

The userdatadir() works very well.

I reduced it to a one liner:

string username = userdatadir().split("\\")[3];
1 Like 1 ·

Write an Answer

Hint: Notify or tag a user in this post by typing @username.

Up to 12 attachments (including images) can be used with a maximum of 23.8 MiB each and 47.7 MiB total.