简体   繁体   中英

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? then i can use this code on other PC's also.

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".

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.

thanks.

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. 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.

string path = Path.Combine(Environment.CurrentDirectory, "Resources");
  1. Get src or root folder path.
  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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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