Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Environment.CurrentDirectory vs System.IO.Directory.GetCurrentDirectory

Tags:

c#

.net

vb.net

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)?

like image 517
John Bustos Avatar asked Nov 14 '13 15:11

John Bustos


2 Answers

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:

  • Created a 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)
  • Edited my .vbproj (or `,csproj') file to set all the files in that directory (including sub-directories) as extra resource files for my project
  • Made a second edit to my .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:

  • Joes solution above
  • This link shows how to be able to add in wildcards to the .vbproj / .csproj files so you can add in full directories at once.
  • This answer which shows how to copy the files to my \Bin Directory

Finally, my solution:

  • I opened my project.vbproj file
  • Above the line <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

like image 72
John Bustos Avatar answered Sep 24 '22 07:09

John Bustos


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);
like image 32
Tim S. Avatar answered Sep 25 '22 07:09

Tim S.