hsim
hsim

Reputation: 2080

Saving files and images in a folder in the project using relative path

In my project I will eventually have to save images created or took on the net and save other created files, which I know how to do. The problem is that when I happen to save my images, I work like this:

using (var imgClient = new WebClient())
{
    Directory.CreateDirectory(imagesDirectory);
    imgClient.DownloadFile(imgUrl, imagesDirectory + m_ObjNameToUse + "(" + abv+ ")" + objNumber + ".jpeg");
}

I call the Directory.CreateDirectory method at this moment to make sure that the directory is always created before creating the image, preventing a crash.

Now my problem is that it uses the string imagesDirectory, which is a hard coded string looking like this:

string imagesDirectory = "C:\\Creation_Folder\\Object_Sets\\Images\\" + _objSetName+ "\\";

That works, as of right now. But I'm under development and the project will eventually be released. I would like to save the images under the "Images" folder in the project, adding the _objSetName as a new folder to distinguish each images created to avoid having too many images in the same file.

So my question is how can I tell my app to locate the Images folder included in the project, and then create a folder under _objSetName?

Upvotes: 1

Views: 14527

Answers (2)

Martin Mulder
Martin Mulder

Reputation: 12954

You could try using your application base folder AppDomain.CurrentDomain.BaseFolder. This is the folder where your application is located. From there it is easy:

 string imagesDirectory = AppDomain.CurrentDomain.BaseFolder + "Object Sets\\Images\\" + _objSetName+ "\\";

Upvotes: 5

Steve Danner
Steve Danner

Reputation: 22158

string imagesDirectory = Path.Combine(Environment.CurrentDirectory, "Images", _objSetName);

Side note, you will probably want to store images in a folder like AppData where your user is guaranteed to have write access. Subdirectories of your application will likely only be accessible by machine administrators.

Upvotes: 2

Related Questions