How to create an empty folder during assembly in Visual Studio 2010?

I have a VS2010 project which should create the dataIn and dataOut directory in the bin \ debug directory to allow some tests to run.

Creating a dataIn directory is simple - just add the contents folder to the project and set the “Copy to output directory” property for each of the files it contains to “Always copy” (or edit the project file to use a wildcard to catch all the files as suggested by other answers to SO) - the directory will be automatically created by VS so that files can be copied to it.

My question is: How can I guarantee that the dataOut directory will be created automatically if necessary?

There are no files in it, so there is nothing for this directory in ItemGroup. If I add the directory entry to the project file manually, I get an error:

"<path>\dataOut" is actually a directory. The "Copy" task does not support copying directories. 

(edit: removed underscores from directory names for italics to work!)

+6
source share
4 answers

Try this in the pre-build phase:

 if not exist "$(TargetDir)dataOut" mkdir "$(TargetDir)dataOut" 

Quotation marks are important if you have spaces somewhere in $(TargetDir) , but I would use them regardless of reliability.

+8
source

One of the ways that I have worked so far is the rather obvious step of adding a dummy text file to a folder and setting its Copy to Output Directory property to Always Copy. I also added a step after the build to remove the dummy file after that, leaving now an empty directory (although for this particular project, which is really just because of my fuss), there would be no impact if it were not there).

I would still like to know if there is a better option though ...

+4
source

what about some mkdirs in the pre-build phase?

0
source

As soon as I shared this dilemma, it occurs to me that the easiest way to deal with this would be to simply create a directory programmatically.

 if(!Directory.Exists(AppDomain.CurrentDomain.BaseDirectory + "yourDirectoryName")) { Directory.CreateDirectory(AppDomain.CurrentDomain.BaseDirectory + "yourDirectoryName"); } 

I'm not sure what type of application you are working with, but winform applications can do this in the main method, and web applications can do this in several places.

0
source

All Articles