在Envdte.solutionFolder中创建新文件,而无需指定完整路径
Envdte80.solutionFolder接口()具有一个addfromfile()方法,但需要现有文件的绝对完整路径。不幸的是,解决方案文件夹对象本身不包含具有磁盘上文件夹路径的属性。
是否有一种简单的方法可以在解决方案文件中创建新的(文本)文件,而无需知道它是完整的绝对路径?我们只想要一个新文件,该文件是文件夹的直接孩子。
这样的东西:
EnvDTE80.SolutionFolder folder;
var file = folder.CreateFile("myFile.txt");
但不是:
EnvDTE80.SolutionFolder folder;
var file = folder.AddFromFile("c:\path_to_folder\myFile.txt");
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
确切地。这是因为解决方案文件夹是虚拟的,它们与文件系统中的任何物理文件夹无关。您可以在解决方案中使用“ myFolder”解决方案文件夹,而文件系统中则没有“ myFolder”。
您还可以为方便起见即可拥有物理文件夹:
但是即使到那时,此物理文件夹与解决方案文件夹绝对无关。解决方案文件夹可能包含来自任何地方的文件,即使是从解决方案根物理文件夹之内。因此,如果要在解决方案文件夹中添加文件,则必须提供完整的路径。
如果您知道在物理文件夹结构中也具有镜像的解决方案文件夹的结构,则可以手动构造完整的路径。使用
envdte80.solution.fullname
获取解决方案的目录名称,将其与envdte80.solutionfolder.parent.name.name
结合使用。Exactly. It's because the solution folders are virtual, they are not related to any physical folders in the file system. You can have "MyFolder" solution folder in your solution and no "MyFolder" in you file system.
You CAN have also the physical folder just for the convenience:
But even then, this physical folder is absolutely unrelated to the solution folder. The solution folder may contain files from anywhere, even from outside the solution root physical folder. That's why you must supply the full path if you want to add a file to a solution folder.
If you know that you have the structure of solution folders mirrored also in physical folder structure, you can construct the full path manually. Get the directory name of the solution with
EnvDTE80.Solution.FullName
, combine it with theEnvDTE80.SolutionFolder.Parent.Name
and then with the file name.