C
C#2y ago
notionhd

Creating Directories for Users

So I want to create a directory when the form loads. But, the issue I'm going to have if another person uses said program is that their computer name is not going to be the same. I searched on the website [ https://learn.microsoft.com/en-us/dotnet/csharp/programming-guide/file-system/how-to-create-a-file-or-folder ] stuff but I couldn't find anything related to creating a directory on a general computer.. Code I have:
private void Form1_Load(object sender, EventArgs e)
{
// A method for setting the colors and other settings.
ColorAndSettings();

// The path of the directory I want to create.
string path = "C:\\Users\\Jay\\Documents\\FileEditor\\Saves";

// A check to see if the directory does not exist.
if (!Directory.Exists(path)) {
// If it don't exist the create the directory at targeted path, set above.
Directory.CreateDirectory(path);
}
}
private void Form1_Load(object sender, EventArgs e)
{
// A method for setting the colors and other settings.
ColorAndSettings();

// The path of the directory I want to create.
string path = "C:\\Users\\Jay\\Documents\\FileEditor\\Saves";

// A check to see if the directory does not exist.
if (!Directory.Exists(path)) {
// If it don't exist the create the directory at targeted path, set above.
Directory.CreateDirectory(path);
}
}
And lets say you used it and your computer name was "Johnny" or something it obviously wouldn't work.. And that's what I need help with.. Any info where I can read about this or can someone just explain how to do such?
6 Replies
230V
230V2y ago
open a command prompt and run set to quickly get a list of all environment variables you can use the userprofile variable instead of hardcoding C:\Users\Jay
notionhd
notionhdOP2y ago
Thank you, I will look into this. If I cannot get it working I'll report back with my issue and updated code.
230V
230V2y ago
a good idea is to also use Path.Combine or Path.Join (depends what the inputs can be) to make this cross-platform, you'd need to choose an environment variable name based on the current OS, https://en.wikipedia.org/wiki/Environment_variable#True_environment_variables
Environment variable
An environment variable is a dynamic-named value that can affect the way running processes will behave on a computer. Environment variables are part of the environment in which a process runs. For example, a running process can query the value of the TEMP environment variable to discover a suitable location to store temporary files, or the HOME ...
230V
230V2y ago
OperatingSystem Class (System)
Represents information about an operating system, such as the version and platform identifier. This class cannot be inherited.
notionhd
notionhdOP2y ago
I will look into this also.
Want results from more Discord servers?
Add your server