Reputation: 151
Ok I know it is simple but I have forgotten how to do it. I want to create a folder in a directory but if I do "C:\Users\George\AppData\Roaming\myprogram"
this only woks for me it will not work with everybody that I send it to
I tried "C:\Users\[User]\AppData\Roaming\Myprogram"
but it says access denied so what can I use to make this work for everyone?
Here is the segment of code I am using to do this if it helps :
public Form2()
{
InitializeComponent();
Directory.CreateDirectory(@"C:\Users\[User]\AppData\Roaming\SkypeAdmin");
Directory.CreateDirectory(@"C:\Users\[User]\AppData\Roaming\SkypeAdmin\mem");
}
and I tried :
public Form2()
{
InitializeComponent();
Directory.CreateDirectory(@"C:\Users\User\AppData\Roaming\SkypeAdmin");
Directory.CreateDirectory(@"C:\Users\User\AppData\Roaming\SkypeAdmin\mem");
}
Upvotes: 0
Views: 610
Reputation: 73502
You can use CommonAppDataPath
string path = Application.CommonAppDataPath;//for folder with version
this may change every version of your application, if you dont want this behavior and you want Same Directory
for all the version of your App you can use this
string path = Directory.GetParent(Application.CommonAppDataPath);
the directory will be readily available when your code executes above line, this is created on demand.
Note: I assume that you want to create some directory for all users and use it. instead of doing so you can used shared Directory and access it from any user.
This is how a sample CommonAppData
path look like
C:\ProgramData\MyCompany\WindowsFormsApplicationTest\1.0.0.0
Typically CommonAppDataBase\CompanyName\ProductName\Version
Upvotes: 2
Reputation: 2372
Your approach predefines a path for the folder so the location would only be valid for you and anyone else who has the specified loacation.
You can try this instead:
public Form1()
{
InitializeComponent();
DirectoryInfo di = Directory.CreateDirectory(skypeAdminPath);
DirectoryInfo di2 = Directory.CreateDirectory(skypeMemPath);
}
string skypeAdminPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "SkypeAdmin\\";
string skypeMemPath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "SkypeAdmin\\mem\\";
Using Path.Combine
:
string skypeAdminPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SkypeAdmin\\");
string skypeMemPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "SkypeAdmin\\mem\\");
Upvotes: 2
Reputation: 22814
You need to use Enviroment.GetFolderPath
for that:
Directory.CreateDirectory(Path.Combine(
Enviroment.GetFolderPath(Enviroment.SpecialFolder.ApplicationData),
@"\SkypeAdmin\"));
Directory.CreateDirectory(Path.Combine(
Enviroment.GetFolderPath(Enviroment.SpecialFolder.ApplicationData),
@"\SkypeAdmin\mem\"));
That does exactly what (I think) you mean to do. Your solution doesn't work because there is no folder called [User]
or User
in the system, so you can't create a folder in there, and Directory.CreateDirectory
doesn't replace that for the user path for you.
Upvotes: 0