【问题标题】:c# - How to assign resources folder to a stringc# - 如何将资源文件夹分配给字符串
【发布时间】: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


【解决方案1】:
  1. 获取src or root文件夹路径。
  2. 将根文件夹路径与您的资源文件夹路径结合起来。
  3. 将结果(根\资源)与您的文件名结合起来

在这里,您将获得文件的确切路径,现在您可以将其复制到目标位置。

这里是实现:这段代码在我的机器上运行。

/// <summary>
/// Here you just need to send fruit name
/// </summary>
/// <param name="fruitName">Name of fruit</param>
public void CopyFile(string fruitName)
{
    string filename = "text.txt"; //setting file name
    string resouceFolderName = Path.Combine("Resources", fruitName);
    //Destination Path
    string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
    //CurrentDirectory return src\\Bin\\Debug so extracting src root folder path
    string parentFolderPath = Directory.GetParent(Directory.GetParent(Environment.CurrentDirectory).FullName).FullName;
    //combining parent folder path with resource folder name
    string folderPath = Path.Combine(parentFolderPath, resouceFolderName); // <--- need to use Resources folder in the project folder here

    //Checking if exist or not
    if (!Directory.Exists(folderPath) || !Directory.Exists(path))
    {
        Console.WriteLine("Error");
        return;
    }

    //filename and location combining to be copied
    string source = Path.Combine(folderPath, filename);
    string destination = Path.Combine(path, filename);

    if (true)
    {
        File.Copy(source, destination, true); //copying
    }
}

注意:这里我使用 test.txtResources 字符串作为常量,因为它们不会改变

【讨论】:

  • 好吧,这行得通。我刚刚将文件夹名称定义为字符串,并将它们从您的代码传递给 fruitName 字符串!并将您的答案也标记为解决方案。谢谢。
【解决方案2】:

Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

您在这里所做的是检索桌面的路径并将 .txt 文件从文件路径复制到那里。 您没有将文件复制到您的项目“资源”文件夹中。

如果您的文件像@Prasad telkikar 所说的那样存在,则使用以下代码,您将获得“资源”文件夹的路径,并能够访问其中的所有内容。

string path = Path.Combine(Environment.CurrentDirectory, "Resources");

【讨论】:

  • CurrentDirectory 将给出 Bin 文件夹的路径,但资源文件夹存在于 csproj 文件所在的位置,您的代码将输出为 \\bin\\Debug\\Resource 不存在..我说的对吗?
  • 假设我在资源文件夹中有橙色、芒果和苹果文件夹,这三个文件夹中的每一个都包含一个名为“text.txt”的文本文件。 &我需要根据要求从每个水果文件夹中复制这些文本文件并将其粘贴到桌面上。现在我需要将“Resources\apple”、“Resources\orange”和“Resources\mango”的位置存储在 3 个不同的字符串上,这样我就可以简单地在“string source = Path.Combine(filepath,filename)”中调用它们部分而不是旧的“文件路径”将这些文本文件从资源文件夹中的任何水果文件夹复制到桌面。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-09-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-04-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多