Click here to Skip to main content
14,638,900 members
Rate this:
Please Sign up or sign in to vote.
See more:
how can i read the text file from resources using this function.

var md5signatures = File.ReadAllLines("mytext.txt");

           if(md5signatures.Contains(thirteenTextBox5.Text))
           {
               label39.Text = "Infected File";
               label39.ForeColor = Color.MediumVioletRed;
           }
           else
           {
               label39.Text = "Your File Is Clean !";
               label39.ForeColor = Color.LimeGreen;
           }


What I have tried:

i have tried properties.resource where the text file name is and gives me error.
Posted
Updated 18-Jul-20 0:16am
Rate this:
Please Sign up or sign in to vote.

Solution 2

read this: [^]
Quote:
String resources are always embedded and cannot be changed; file resources are always linked and cannot be changed.
A. adding a file using the Resource editor copies the content of the file into your Project as a static element: the file you added might change, but, your resource (resx) content will not.

B. assuming that the file has content that changes, and, that you always want to be processing the current content:

B.1. create an embedded resource from a file:

add a new, or existing, Text file to your Project

right click on the file in the Solution Explorer, and set the Build Action for this file to 'Embedded Resource," with, usually, the "Copy if newer" setting.

B.2. to read the file contents at runtime:

// code from: [^] by dtb
// required
using System.IO;
using System.Reflection;

public string GetEmbeddedResource(string namespacename, string filename)
{
    var assembly = Assembly.GetExecutingAssembly();
    var resourceName = namespacename + "." + filename;

    using (Stream stream = assembly.GetManifestResourceStream(resourceName))
    using (StreamReader reader = new StreamReader(stream))
    {
        string result = reader.ReadToEnd();
        return result;
    }
}
Sample call from actual code:
string s = GetEmbeddedResource("SociometryTree", "Notes.txt");
   
v2
Comments
Anthony Raven 18-Jul-20 7:45am
   
thank you works perfect!!!!!!
BillWoodruff 19-Jul-20 17:17pm
   
glad that's helpful ! i suggest you wrap the code in a try/catch block and/or implement error handling for safety.
Rate this:
Please Sign up or sign in to vote.

Solution 1

I add two text files automatically to every project: the TODO list, and the modification history. These can then be referenced in the About dialog so users can see what changes were made.
To add another, Open your Project in the Solution pane, and double click "Properties".
In the new dialog, select "Resources" on the left, and create a resources file if necessary.
From the menu tools, click the down arrow beside "Add Resource" and select "Add existing file". Select your file, and click "Open".
Close the Properties page, and go to your code.
You can now access the file content:
string s = Properties.Resources.NameOfYourTextFileMinusTheExtension;
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100