在Envdte.solutionFolder中创建新文件,而无需指定完整路径

发布于 2025-02-05 22:21:57 字数 602 浏览 1 评论 0 原文

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");

The EnvDTE80.SolutionFolder interface (https://learn.microsoft.com/en-us/dotnet/api/envdte80.solutionfolder) has an AddFromFile() method, but requires the absolute full path of an existing file. Unfortunately the solution folder object itself doesn't contain a property with the path of the folder on disk.

Is there an easy way to create a new (text) file in a SolutionFolder without needing to know it's full absolute path? We just want a new file which is a direct child of the folder.

Something like this:

EnvDTE80.SolutionFolder folder;
var file = folder.CreateFile("myFile.txt");

But not:

EnvDTE80.SolutionFolder folder;
var file = folder.AddFromFile("c:\path_to_folder\myFile.txt");

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

朕就是辣么酷 2025-02-12 22:21:57

不幸的是,解决方案文件夹对象本身不包含带有磁盘上文件夹路径的属性。

确切地。这是因为解决方案文件夹是虚拟的,它们与文件系统中的任何物理文件夹无关。您可以在解决方案中使用“ myFolder”解决方案文件夹,而文件系统中则没有“ myFolder”。

您还可以为方便起见即可拥有物理文件夹:

C:\Solution
   MyFolder
      myFile.txt

但是即使到那时,此物理文件夹与解决方案文件夹绝对无关。解决方案文件夹可能包含来自任何地方的文件,即使是从解决方案根物理文件夹之内。因此,如果要在解决方案文件夹中添加文件,则必须提供完整的路径。

如果您知道在物理文件夹结构中也具有镜像的解决方案文件夹的结构,则可以手动构造完整的路径。使用 envdte80.solution.fullname 获取解决方案的目录名称,将其与 envdte80.solutionfolder.parent.name.name 结合使用。

Unfortunately the solution folder object itself doesn't contain a property with the path of the folder on disk.

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:

C:\Solution
   MyFolder
      myFile.txt

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 the EnvDTE80.SolutionFolder.Parent.Name and then with the file name.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文