2

I have the app called osc and i want to create folder in root path and save image to that folder.

I want to save images at folder like "osc/Screenshots/EmpName/EmpId/scr.jpg"

how to create folder and save image on this path

help me

thanks in advance

Saurabh Solanki
  • 2,146
  • 18
  • 31

2 Answers2

6

To get the string representing the path:

string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);

Probably you want to do something like the following:

DirectoryInfo directory = new DirectoryInfo(path);

To create a new path based on root folder:

var imagePath = path + @"\MyAppName\Images"

if (!Directory.Exists(imagePath)) 
{   
   DirectoryInfo di = Directory.CreateDirectory(imagePath);
}

EDIT: As alternative you may use specials folders. For example LocalApplicationData, in this folder you can place machine scoped data of your application per user. Below the variable localData will be

C:\Users\USER_NAME\AppData\Local

where USER_NAME is the user profile.

string localData = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData);
var imagePath = localData + @"\Images";

if (!Directory.Exists(imagePath)) 
{   
    DirectoryInfo di = Directory.CreateDirectory(imagePath);
}
E-Bat
  • 4,792
  • 1
  • 33
  • 58
  • it gives me the path of my exe like osc/bin/release/screenshots.... but i want to create folder in root directory osc/. – Saurabh Solanki Oct 03 '16 at 13:39
  • yes but it returns bin/release folder path i want root path of app – Saurabh Solanki Oct 03 '16 at 14:11
  • 1
    @SaurabhSolanki, when you launch your app from Visual Studio, the root is the Release or Debug folder. When you install your application, then the root folder will be the installation folder. – E-Bat Oct 03 '16 at 14:24
  • thank you.....@ E-Bat Yes, it relolved my issue and it saves screenshots to the root directory of my project , but the problem is occured that after installing the application the app is installed in c:/programfiles(86)/projectfolder drive so my app can't access to that folder to write image files. due to C drive is unaccessible. – Saurabh Solanki Oct 05 '16 at 06:02
  • @SaurabhSolanki, you can use the specials folders available in windows. .NET provides a consistent way to access to those folders. For example ApplicationData. Have a look to my edit in the post. – E-Bat Oct 05 '16 at 12:49
2

AppDomain.CurrentDomain.BaseDirectory will give you the path to your application folder.

rbr94
  • 2,227
  • 3
  • 23
  • 39