I'm writing a .Net WinForms on and constantly switching between DEBUG and RELEASE configurations and have a few files I need either configuration to be able to get to.
What I was thinking to do was to put the files in a common directory in the BIN folder so it would look like this:
MyProject/Bin/CommonFiles
MyProject/Bin/Debug
MyProject/Bin/Release
And I was thinking about accessing the files using something along the lines of:
System.IO.Directory.GetParent(System.IO.Directory.GetCurrentDirectory).FullName
My question is if this is dangerous since, from what I've read, System.IO.Directory.GetCurrentDirectory
might change due to the user selecting a new current directory in, say, an open file dialogue box.
Should I rather use something along the lines of:
System.IO.Directory.GetParent(Environment.CurrentDirectory).FullName
OR is there an even better way to get to the /Bin
folder so I can move from there or a generally accepted way / location to store files the program usually needs to reach and way to reference that more easily (maybe something that's made to work with any kind of app and not just WinForms)?
Although most of the answers did actually seem to work, the solution offered by Joe above, after quite a bit of tweaking, proved to be the best solution for my situation.
Firstly, the final solution I went with:
Common Files
directory (In my case, at the same level as my Bin
directory, but that's not necessary, it'll just explain how the code I'll put up later works).vbproj
(or `,csproj') file to set all the files in that directory (including sub-directories) as extra resource files for my project.vbproj
file to copy all those resource files to my bin\Debug or bin\Release directory at build time.Secondly, how I learned to do this:
Finally, my solution:
<Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
, I added in one new ItemGroup
looking as follows:<ItemGroup> <CommonFiles Include="Common Files\**" /> </ItemGroup>
This loads in all the files (including all sub-folders) and gives I the Build Action CommonFiles
Beneath the line <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />
, I added in the following lines:
<PropertyGroup> <PrepareForRunDependsOn>$(PrepareForRunDependsOn);CopyCommonFiles</PrepareForRunDependsOn> </PropertyGroup>
<Target Name="CopyCommonFiles"> <Copy SourceFiles="@(CommonFiles)" DestinationFolder="$(OutDir)\%(RecursiveDir)" SkipUnchangedFiles="true"/> </Target>
This will copy the files from the CommonFiles
build action into your bin\Debug / bin\Release directories at build time.
And that's how I did it....
ANY comments / thoughts are really greatly appreciated )if you think I should have used another way or anything else)....
this
You can get the .exe location of your app with System.Reflection.Assembly.GetExecutingAssembly().Location
.
string exePath = System.Reflection.Assembly.GetExecutingAssembly().Location;
string exeDir = System.IO.Path.GetDirectoryName(exePath);
DirectoryInfo binDir = System.IO.Directory.GetParent(exeDir);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With