如何在delphi中获取当前用户的名称? [英] How to get the name of the current user in delphi?

查看:170
本文介绍了如何在delphi中获取当前用户的名称?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我在Windows 8中使用带有XE3的delphi FM2。

Hi there i am using delphi FM2 with XE3 in windows 8.

问题是我希望用户按下按钮,然后导航到子文件夹位于appdata ex。 C:\Users\Kobus\AppData\Roaming.minecraft

The problem im having is that i want the user to press a button and then navigate to a subfolder located in appdata ex. C:\Users\Kobus\AppData\Roaming.minecraft

每个人的用户名都不同,因此不会起作用。

Everybody has a diferant username so this wont work.

所以我使用以下代码获取用户名:

So i use this code to get the username:

function GetCurrentUserName : string;
const
  cnMaxUserNameLen = 254;
var
  sUserName     : string;
  dwUserNameLen : DWord;
begin
  dwUserNameLen := cnMaxUserNameLen-1;
  SetLength( sUserName, cnMaxUserNameLen );
  GetUserName(PChar( sUserName ),dwUserNameLen );
  SetLength( sUserName, dwUserNameLen );
  Result := sUserName;
end;

username := GetCurrentUserName;

然后我说 ShowMessage('C:\Users\'+ username +'\AppData\Roaming\.minecraft\saves\'); 检查输出。

然后输出我得到的是:'C:\Users\Kobus',由于某种原因,其余的路径名都丢失了。

And the output i get is : 'C:\Users\Kobus' for some reason the rest of the path name is lost.

我需要显示的是:' C:\Users\'Kobus'\AppData\Roaming.minecraft\saves\'

What i need to be displayed is : 'C:\Users\'Kobus'\AppData\Roaming.minecraft\saves\'

谢谢。

推荐答案

问题是 dwUserNameLen 包含字符串的长度,包括结尾的零终止符。因此,当您这样做时:

The problem is that dwUserNameLen contains the length of the string, including the trailing zero terminator. So when you do:

SetLength(sUserName, dwUserNameLen);

这将导致 sUserName 设置为'Kobus#0'。然后,在某个时候将其传递给Windows API对话框函数,该函数将字符串视为以null终止的字符串,并在杂散的null终止符处截断该字符串。

this results in sUserName being set to 'Kobus#0'. At some point you then pass this to a Windows API dialog function that treats the string as a null-terminated string, and truncates the string at the stray null-terminator.

因此,您可以这样修复它:

So you fix it like this:

SetLength(sUserName, dwUserNameLen-1);

请注意,还应检查 GetUserName 如果调用失败:

Note that you should also check the return value of GetUserName in case that call fails:

if not GetUserName(PChar(sUserName), dwUserNameLen) then
  RaiseLastOSError;

或更简洁的变量:

Win32Check(GetUserName(PChar(sUserName), dwUserNameLen));

最后一点。这是获取漫游应用程序数据文件夹的错误方法。首先,您假设各种实现细节。您的方法将在使用不同命名模式的Windows的较旧版本上失败。或某些将来的Windows版本。或以其他方式配置的当前版本。

One final point. This is the wrong way to get hold of the roaming app data folder. For a start you are assuming all sorts of implementation details. Your approach will fail on older versions of Windows which use different naming patterns. Or some future version of Windows. Or the current versions that have been configured in a different way.

执行此操作的正确方法是询问系统漫游应用程序数据文件夹在哪里。使用 CSIDL_APPDATA (对于旧的Windows版本)或 FOLDERID_RoamingAppData (对于现代Windows版本)执行此操作。

The right way to do this is to ask the system where the roaming app data folder is. Do that using CSIDL_APPDATA (for older Windows versions), or FOLDERID_RoamingAppData (for modern Windows versions).

这篇关于如何在delphi中获取当前用户的名称?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆