解决方案中包含的文件的本地路径 [英] Local paths of files contained in a solution

查看:116
本文介绍了解决方案中包含的文件的本地路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要从解决方案中的文件夹内包含的文本文件中读取数据(已将其添加到解决方案中,并且可以从解决方案资源管理器"中进行访问).我知道如何像这样访问它...

I need to read in data from a text file that is contained inside of a folder in my solution (It has been added to the solution and is accessible from the Solution Explorer). I know how to access it like this...

_prefixParts = PopulateFromFile(
    @"C:\Users\Owner\Desktop\AoW\AoW\AoW\Utility\Names\namePrefix.txt");

有没有一种方法可以写入解决方案本地的文件路径?我已经尝试了所有我能想到的并且没有设法做到的事情.

Is there a way to write the file path that is local to the solution? I've tried everything that I can think of and haven't managed to do it.

此外,如果我要将文本文件作为嵌入式资源,这会变得重要吗?

Also, If I were to make that text file an embedded resource, would this even matter?

推荐答案

是.您可以使其成为嵌入式资源.或者更好的是,使其成为资源( .resx ).

Yes. You can just make it an embedded resource. Or better yet, make it a resource (.resx).

要回答第一个问题:您可以使用相对路径访问该文件.因此,假设可执行文件在您的bin \ Debug文件夹中.这意味着您需要向上移动两次,以便位于项目文件夹的根目录下.

To answer your first question: you can use relative paths to access that file. So, assuming that the executable is in your bin\Debug folder. It means that you need to go up twice so that you're inside the project folder's root.

例如.您的文件夹结构为:

Eg. Your folder structure is:

  • SolutionName \ ProjectName \ bin \ debug->可执行文件的位置
  • SolutionName \ ProjectName \ Folder1 \ Folder2-> namePrefix.txt
  • 的位置
  • SolutionName\ProjectName\bin\debug --> location of you executable
  • SolutionName\ProjectName\Folder1\Folder2 --> location of your namePrefix.txt

您需要的是..\..\Folder1\Folder2\namePrefix.txt

我只想强调一下,如果您将应用程序作为Windows服务运行,则此方法将无效.更好的是,为该文件使用资源.

I would just like to emphasize that this won't work if you run your app as a Windows Service. So better yet, use a resource for that file.

这篇关于解决方案中包含的文件的本地路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆