Click here to Skip to main content
15,867,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!
I need some quick help to create an application in C# that works on all useraccounts and will copy a text file from C:\Users\Techhubcoding\AppData\Local\test.txt to C:\Users\Techhubcoding\Desktop\test

My userprofile is "Techhubcoding" and i want this to work for all computerprofiles.
How do i define the "computer profilename" in a filepath?

What I have tried:

I have tried this:
C#
File.Copy(@"C:\Users\Techhubcoding\AppData\Local\test.txt", @"C:\Users\Techhubcoding\Desktop\test\test.txt");

The file was copied to the location but what i need help with is to make the filepath work for any user.

I have also tried Environment.ExpandEnvironmentVariables and Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) but i didn't manage to get that work.

I would be very grateful for any help! (;
Thanks!
Posted
Updated 30-Oct-19 9:27am
v2

You can use:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

See: Environment.SpecialFolder Enum (System) | Microsoft Docs[^]
An example:
string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
string destFilename = Path.Combine(userPath, "test.txt");
File.Copy("test.txt", destFilename);
 
Share this answer
 
v2
Comments
Techhubcoding 30-Oct-19 15:02pm    
I still don't understand how it works...
Could you give an example please?
Techhubcoding 30-Oct-19 15:24pm    
Thanks!!!
Techhubcoding 30-Oct-19 15:35pm    
The end result that works for me!
string userPath = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
File.Copy(userPath + @"\Appdata\Local\test.txt", userPath + @"\Desktop\test\test.txt");
OriginalGriff 30-Oct-19 15:42pm    
You shouldn't do that!
https://docs.microsoft.com/en-us/dotnet/api/system.environment.specialfolder?view=netframework-4.8

"The user's profile folder. Applications should not create files or folders at this level; they should put their data under the locations referred to by ApplicationData."
Try:
Environment.GetFolderPath(Environment.SpecialFolder.UserProfile)

it will return the user base folder:
C#
C:\Users\Techhubcoding
Or:
C:\Users\PaulG
as appropriate.
 
Share this answer
 
Comments
Techhubcoding 30-Oct-19 15:09pm    
But how do i define the file location after the return to the base folder?
Could you give an example please.
Thanks for the solution!

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900