Click here to Skip to main content
15,897,371 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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

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");
 
Share this answer
 
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.
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;
 
Share this answer
 

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900