【发布时间】:2017-11-19 01:39:17
【问题描述】:
我正在使用这个,
string finename = "text.txt"; //setting file name
//setting locations
string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
string filepath = @"C:\User\Users\Documents\Files\Apple"; // <--- need to use Resources folder in the project folder here
//filename and location combining to be copied
string source = Path.Combine(filepath,filename);
string destination = Path.Combine(path,filename);
if (something=1)
{
File.Copy(source,destination, true); //copying
}
我已将所有文件添加到资源中,现在我需要在此处引用资源文件夹而不是“文件路径”, 有什么方法可以将资源文件夹(及其内容)分配给一个字符串,这样我就可以简单地更改位置?然后我也可以在其他 PC 上使用此代码。
编辑 -
假设我在资源文件夹中有橙色、芒果和苹果文件夹,这三个文件夹中的每一个都包含一个名为“text.txt”的文本文件。
我需要根据要求从每个水果文件夹中复制其中一个文本文件并将其粘贴到桌面上。
现在我需要在 3 个不同的字符串上存储 "Resources\apple" 、 "Resources\orange" 和 "Resources\mango" 的位置,这样我就可以简单地在 "string source = Path.Combine(filepath,文件名)”部分而不是旧的“文件路径”,将这些文本文件从资源文件夹中的任何水果文件夹复制到桌面。
谢谢。
【问题讨论】:
-
您的资源文件夹是否存在于您的项目当前目录中?或直截了当的问题,您的资源文件夹在哪里?
-
是的,它存在于我当前的项目目录中。
-
所以你可以使用
string path = Path.Combine(Environment.CurrentDirectory, "Resources")来获取你的资源目录路径。 -
我试过了,当我把 string path = Path.Combine(Environment.CurrentDirectory, "Resources\apple");它在“\”苹果处给了我错误;
-
当我把...?接下来是什么..?您正在访问文件,对吗?您还需要提供扩展名。你是否在合并函数的第二个参数中给出扩展名
标签: c# string resources location