实际上,程序要求是这样的。
每个用户都有一个对应的数据目录。例如,在XP系统下,一般是C:\Document and Settings\<username>。
实际上问题想得到的结果是,在用户 UserA 登录系统后,使用用户 UserB 的身份启动程序pro.exe。那么使用什么方法,在程序pro.exe中,仍旧得到C:\Document and Settings\UserA这个目录,而不是C:\Document and Settings\UserB这个目录?
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Runtime.InteropServices;
namespace WindowsApplication25
{
public partial class Form1 : Form
{
[DllImport("Advapi32.dll", EntryPoint = "GetUserName",
ExactSpelling = false, SetLastError = true)]
public Form1()
{
InitializeComponent();
System.Text.StringBuilder b = new System.Text.StringBuilder(100);
int n = b.Capacity;
byte[] str = new byte[256];
Int32[] len = new Int32[1];
len[0] = 256;
GetUserName(str, len);
MessageBox.Show(System.Text.Encoding.ASCII.GetString(str));
string a;
a = System.Security.Principal.WindowsIdentity.GetCurrent().Name.ToString();