本文介绍了Environment.CurrentDirectory VS System.IO.Directory.GetCurrentDirectory的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在写一个.net的WinForms上调试和发布配置之间不断切换,并有我需要的任一配置才能够得到一些文件。

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.

我在想什么做的是将文件放在一个公共目录bin文件夹所以它是这样的:

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

我的问题是,如果这是危险的,因为,从我读过, System.IO.Directory.GetCurrentDirectory 可能会改变,由于用户选择一个新的当前目录在,比方说,一个打开的文件对话框。

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

有一个更好的方式来获得的 /斌文件夹,这样我就可以从那里移动或一个普遍接受的方法/位置存储文件的程序通常需要达到和方式来引用更容易(也许东西是做与任何类型的应用程序的工作,而不是仅仅的WinForms)?

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:

  • 创建一个共同文件目录(在我的情况下,在同一水平作为我的目录,但是这没有必要的,它只会解释如何code我就忍了后来的作品)
  • 编辑我的 .vbproj (或`的csproj)文件设置该目录中的所有文件(包括子目录)作为额外的资源文件为我的项目
  • 在做了第二次的编辑给我的 .vbproj 文件,所有这些资源文件复制到我的Bin \ Debug或斌\ Release目录在编译的时候。
  • 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

最后,我的解决办法:

  • 在我睁开project.vbproj文件
  • 上面< ,我加入;引进项目=$(MSBuildToolsPath)\ Microsoft.VisualBasic.targets/&GT一种新的 ItemGroup 寻找如下:
  • I opened my project.vbproj file
  • Above the line <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />, I added in one new ItemGroup looking as follows:

&LT; ItemGroup&GT; &LT; CommonFiles包括=共同文件\ **/&GT; &LT; / ItemGroup&GT;

本负载中的所有文件(包括所有子文件夹),并给我生成操作 CommonFiles

This loads in all the files (including all sub-folders) and gives I the Build Action CommonFiles

  • 下方&LT;导入项目=$(MSBuildToolsPath)\ Microsoft.VisualBasic.targets/&GT; ,我加了下面几行:

  • Beneath the line <Import Project="$(MSBuildToolsPath)\Microsoft.VisualBasic.targets" />, I added in the following lines:

&LT;的PropertyGroup&GT; <$p$ppareForRunDependsOn>$($p$ppareForRunDependsOn);CopyCommonFiles</$p$ppareForRunDependsOn> &LT; /的PropertyGroup&GT;

&lt;目标名称=CopyCommonFiles&GT; &LT;复制的源文件=@(CommonFiles)DestinationFolder =$(OUTDIR)\%(RecursiveDir)SkipUnchangedFiles =真/&GT; &LT; /目标&GT;

这将从复制的文件的 CommonFiles 建设行动统一到你的bin \调试/ BIN \发布目录在编译的时候。

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....

任何意见/想法真的是很大的AP preciated),如果你觉得我应该用另一种方式或其他任何东西)......

ANY comments / thoughts are really greatly appreciated )if you think I should have used another way or anything else)....

这篇关于Environment.CurrentDirectory VS System.IO.Directory.GetCurrentDirectory的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-21 01:20