5

I am new in mvc and c# and I can't solve following problem:

I am trying to create a folder named "Items" in solution folder. I have tryed to use CreateDirectory method:

Directory.CreateDirectory("~/Images");

But it didn't work for me - folder wasn't created ..

Partly working solution was to create a folder by :

Directory.CreateDirectory(Server.MapPath("~/Images"));
  • "Items" folder was created, but it is not included in the solution:

enter image description here

How to create folder in solution directory so that it is included in project ?

(I needs to by done by code not by hand)

5
  • Out of interest, why are you trying to do this?
    – Richard Ev
    Commented Jan 4, 2016 at 15:20
  • 4
    Your solution file is just a structured container for the files contained within it. Any folders created during runtime will have no knowledge of your solution and cannot be made part of the solution.
    – jvanrhyn
    Commented Jan 4, 2016 at 15:22
  • Thx for quick responses. I need folder to store uploaded images. Could you provide any hint for good approach to do that ? thx
    – fxdx
    Commented Jan 4, 2016 at 15:26
  • I mean - approach to specify folder, where to store images uploaded by users. thx
    – fxdx
    Commented Jan 4, 2016 at 15:36
  • Like @RichardEverett asked, why not just use the wwwroot, it has an image folder, Commented Oct 29, 2018 at 16:32

3 Answers 3

6

You need to understand what solution and csproj file is used for

In general, they're being designed and used for development with Visual Studio, and once the project is compiled, all these files will be ignored and excluded from the deployment package

Directory.CreateDirectory(Server.MapPath("~/Images"));

The code above simply create the directory if not existed yet in the deployment package at run-time, so you won't see it in your solution unless you run the project locally (either debug/release mode, it does not matter here). However, everything will run normally in hosted environment (ex: IIS).

For your information, here's the brief of what solution and csproj is

  • solution (.sln) file: contains information to manage one or many individual projects, contains build environments (for each project), start up mode (useful when you want to start multiple projects in one run), project dependencies and so on. Take a note that VS also read from suo file (solution user options) which is used to defined user-custom preferences (you should not include the .suo file in the version control, because it's custom settings)

  • csproj file: define the structures of project, what the namespace is, what is static folders, embedded resources, references, packages, etc.

Lastly, if you create the folder manually, VS will auto include that folder into deployment package AND csproj, but depends on the file type, you might need to change the Build Action and Copy To Output Directory in file properties.

Hope it helps.

4

A deployed web application on a web server doesn't have any notion of Visual Studio solution or projects. So the Directory.CreateDirectory(Server.MapPath("~/Images")) is the correct way to create a folder inside your web application at runtime but we cannot be talking about including it into a solution because this hardly makes sense in a pre-compiled web application. If you create the directory on your local development machine, you could always manually include the folder to the corresponding .csproj file, but at runtime this will not make any difference whatsoever.

1

The reason I wanted to create a folder (if didn't exist) was to make sure it exits before I try to store image in it.

After reading posts here and a few google searches I have concluded that the proper way to handle image upload would be

  1. To create (In my case) folder "Images" by hand to be sure it exists
  2. Then storing uploaded img in existing folder:
string path =Server.MapPath("~/Images/"+ UploadedImageName);
file.SaveAs(path);
1
  • as in your original question, you want to create the folder using code, I recommend you to do so, and verify the folder existence by using Directory.Exists(path)
    – Kien Chu
    Commented Jan 5, 2016 at 2:58

Not the answer you're looking for? Browse other questions tagged or ask your own question.