userprofile path returns Default while running as a schedule task

702 views Asked by At

I have created following C# console app:

var userName = System.Security.Principal.WindowsIdentity.GetCurrent().Name;
var path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
var path2 = Environment.ExpandEnvironmentVariables("%userprofile%");

File.AppendAllText(@"D:\logs\info.txt", userName + " -- " + path + " -- " + path2);

When I create a scheduled task using Windows Task Scheduler and set the user account to run the task to my account (ht-laptop\huseyin), I am getting the following output in info.txt file:

ht-laptop\huseyin -- C:\Users\Default\Documents -- C:\Users\Default

This seems to be random though, I had seen cases where the printed text was as follows:

ht-laptop\huseyin -- C:\Users\huseyin\Documents -- C:\Users\huseyin

Any idea why this happens?

1

There are 1 answers

1
Andrew Van Til On

Are you running this on Windows 8+ (or similar)? If so, this is a known issue with user profile loading. The technote (kb2968540) has a workaround (which is kind of kludgy IMO).