简体   繁体   English

CodeGo.net>如何分配资源文件夹的字符串

[英]c# - How to assign resources folder to a string

I'm currently using this, 我目前正在使用

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
}

I've added all the files to the Resources now i need to refer to Resources folder instead of "filepath" here, is there any way to assign the resources folder (& its contents) to a string so i can simply change the location? 我已经将所有文件添加到资源中,现在我需要在这里引用资源文件夹而不是“ filepath”,有什么办法可以将资源文件夹(及其内容)分配给一个字符串,以便我可以简单地更改位置? then i can use this code on other PC's also. 然后我也可以在其他PC上使用此代码。

Edit - 编辑-

Imagine i have orange,mango and apple folder inside the resource folder, and each of these 3 folders contain a text file with the name "text.txt". 想象一下,我在资源文件夹中有orange,mango和apple文件夹,并且这3个文件夹中的每一个都包含一个名为“ text.txt”的文本文件。

And i need to copy one of these text files from each & every fruit folder on request & paste it on desktop. 我需要根据需要从每个水果文件夹复制这些文本文件之一,并将其​​粘贴到桌面上。

Now i need to store the location of "Resources\\apple" , "Resources\\orange" & "Resources\\mango" on 3 different strings so i can simply call them in the "string source = Path.Combine(filepath,filename)" part instead of older "filepath" to copy those text files from any of the fruit folder inside resources folder to the desktop. 现在,我需要将“ Resources \\ apple”,“ Resources \\ orange”和“ Resources \\ mango”的位置存储在3个不同的字符串上,以便我可以在“字符串source = Path.Combine(filepath,filename)”中简单地调用它们部分而不是较旧的“文件路径”将这些文本文件从资源文件夹内的任何水果文件夹复制到桌面。

thanks. 谢谢。

Environment.GetFolderPath(Environment.SpecialFolder.Desktop); Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

What you are doing here is retrieving the path to your desktop and copying your .txt file from your filepath to there. 您在这里所做的是获取桌面的路径,并将.txt文件从文件路径复制到该路径。 You are not copying the file onto your project "Resources" folder. 您没有将文件复制到项目的“资源”文件夹中。

In case your file is there as @Prasad telkikar said, with the following code you would have the path to your's "Resource" folder and be able to access all the content inside it. 如果您的文件如@Prasad telkikar所说的那样存在,则使用以下代码,您将具有您“ Resource”文件夹的路径,并能够访问其中的所有内容。

string path = Path.Combine(Environment.CurrentDirectory, "Resources");
  1. Get src or root folder path. 获取src or root文件夹路径。
  2. Combine root folder path with your resource folder path. 将根文件夹路径与资源文件夹路径合并。
  3. Combine result(root\\resources) with your file name 将结果(根\\资源)与您的文件名合并

Here you go, you will get exact path of file and now you can copy it to the destination. 在这里,您将获得文件的确切路径,现在可以将其复制到目标位置。

Here is the implementation: This code is running on my machine. 这是实现:此代码在我的计算机上运行。

/// <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
    }
}

Note: Here I used test.txt and Resources string as a constant considering they won't change 注意:在这里,我将test.txtResources字符串用作常量,因为它们不会更改

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM