Can't read html file in c# - c#

I'm freaking out with this problem. I want to read the data of a HTML in c#. So, I have an HTML file in the same Class Library in Visual Studio. Visual Studio Class Library distribution
So, I want to use Stream Reader to read the file, with the assembly.
StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("proyectoHADS.LIbreriaClase.EmailTemplate.html"));
But, this returns a null, so I use the GetManifestResourceNames to check if it exists, but it doesn't because the array is empty. I don't know why I can't get the HTML. The entire code is here:
namespace LibreriaClase
public class EnviarCorreo {
public void enviarCorreo(String correo)
{
....
var assembly = Assembly.GetExecutingAssembly();
string[] names = assembly.GetManifestResourceNames();
StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("proyectoHADS.LIbreriaClase.EmailTemplate.html"));
body = reader.ReadToEnd();`

I hope this code snippet can help you:
public void enviarCorreo(String correo)
{
....
var assembly = Assembly.GetExecutingAssembly();
string[] names = assembly.GetManifestResourceNames();
using( StreamReader reader = new StreamReader(assembly.GetManifestResourceStream("proyectoHADS.LIbreriaClase.EmailTemplate.html"));
{
String line = String.Emtpy;
while( (line = reader.ReadLine()) != null )
{
Console.WriteLine(line);
}
}
By the way dont forget check proyectoHADS.LIbreriaClase.EmailTemplate.html line is it null or not.

Related

How to access resources by name in c# [duplicate]

How do I read an embedded resource (text file) using StreamReader and return it as a string? My current script uses a Windows form and textbox that allows the user to find and replace text in a text file that is not embedded.
private void button1_Click(object sender, EventArgs e)
{
StringCollection strValuesToSearch = new StringCollection();
strValuesToSearch.Add("Apple");
string stringToReplace;
stringToReplace = textBox1.Text;
StreamReader FileReader = new StreamReader(#"C:\MyFile.txt");
string FileContents;
FileContents = FileReader.ReadToEnd();
FileReader.Close();
foreach (string s in strValuesToSearch)
{
if (FileContents.Contains(s))
FileContents = FileContents.Replace(s, stringToReplace);
}
StreamWriter FileWriter = new StreamWriter(#"MyFile.txt");
FileWriter.Write(FileContents);
FileWriter.Close();
}
You can use the Assembly.GetManifestResourceStream Method:
Add the following usings
using System.IO;
using System.Reflection;
Set property of relevant file:
Parameter Build Action with value Embedded Resource
Use the following code
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}
resourceName is the name of one of the resources embedded in assembly.
For example, if you embed a text file named "MyFile.txt" that is placed in the root of a project with default namespace "MyCompany.MyProduct", then resourceName is "MyCompany.MyProduct.MyFile.txt".
You can get a list of all resources in an assembly using the Assembly.GetManifestResourceNames Method.
A no brainer astute to get the resourceName from the file name only (by pass the namespace stuff):
string resourceName = assembly.GetManifestResourceNames()
.Single(str => str.EndsWith("YourFileName.txt"));
A complete example:
public string ReadResource(string name)
{
// Determine path
var assembly = Assembly.GetExecutingAssembly();
string resourcePath = name;
// Format: "{Namespace}.{Folder}.{filename}.{Extension}"
if (!name.StartsWith(nameof(SignificantDrawerCompiler)))
{
resourcePath = assembly.GetManifestResourceNames()
.Single(str => str.EndsWith(name));
}
using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
or as an async extension method:
internal static class AssemblyExtensions
{
public static async Task<string> ReadResourceAsync(this Assembly assembly, string name)
{
// Determine path
string resourcePath = name;
// Format: "{Namespace}.{Folder}.{filename}.{Extension}"
if (!name.StartsWith(nameof(SignificantDrawerCompiler)))
{
resourcePath = assembly.GetManifestResourceNames()
.Single(str => str.EndsWith(name));
}
using Stream stream = assembly.GetManifestResourceStream(resourcePath)!;
using StreamReader reader = new(stream);
return await reader.ReadToEndAsync();
}
}
// Usage
string resourceText = await Assembly.GetExecutingAssembly().ReadResourceAsync("myResourceName");
You can add a file as a resource using two separate methods.
The C# code required to access the file is different, depending on the method used to add the file in the first place.
Method 1: Add existing file, set property to Embedded Resource
Add the file to your project, then set the type to Embedded Resource.
NOTE: If you add the file using this method, you can use GetManifestResourceStream to access it (see answer from #dtb).
Method 2: Add file to Resources.resx
Open up the Resources.resx file, use the dropdown box to add the file, set Access Modifier to public.
NOTE: If you add the file using this method, you can use Properties.Resources to access it (see answer from #Night Walker).
Basically, you use System.Reflection to get a reference to the current Assembly. Then, you use GetManifestResourceStream().
Example, from the page I posted:
Note: need to add using System.Reflection; for this to work
Assembly _assembly;
StreamReader _textStreamReader;
try
{
_assembly = Assembly.GetExecutingAssembly();
_textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt"));
}
catch
{
MessageBox.Show("Error accessing resources!");
}
In Visual Studio you can directly embed access to a file resource via the Resources tab of the Project properties ("Analytics" in this example).
The resulting file can then be accessed as a byte array by
byte[] jsonSecrets = GoogleAnalyticsExtractor.Properties.Resources.client_secrets_reporter;
Should you need it as a stream, then ( from https://stackoverflow.com/a/4736185/432976 )
Stream stream = new MemoryStream(jsonSecrets)
When you added the file to the resources, you should select its Access Modifiers as public than you can make something like following.
byte[] clistAsByteArray = Properties.Resources.CLIST01;
CLIST01 is the name of the embedded file.
Actually you can go to the resources.Designer.cs and see what is the name of the getter.
adding e.g. Testfile.sql
Project Menu -> Properties -> Resources -> Add Existing file
string queryFromResourceFile = Properties.Resources.Testfile.ToString();
I know it is an old thread, but this is what worked for me :
add the text file to the project resources
set the access modifier to public, as showed above by Andrew Hill
read the text like this :
textBox1 = new TextBox();
textBox1.Text = Properties.Resources.SomeText;
The text that I added to the resources: 'SomeText.txt'
Something I learned just now is that your file is not allowed to have a "." (dot) in the filename.
Templates.plainEmailBodyTemplate-en.txt --> Works!!!
Templates.plainEmailBodyTemplate.en.txt --> doesn't work via GetManifestResourceStream()
Probably because the framework gets confused over namespaces vs filename...
You can also use this simplified version of #dtb's answer:
public string GetEmbeddedResource(string ns, string res)
{
using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format("{0}.{1}", ns, res))))
{
return reader.ReadToEnd();
}
}
By all your powers combined I use this helper class for reading resources from any assembly and any namespace in a generic way.
public class ResourceReader
{
public static IEnumerable<string> FindEmbededResources<TAssembly>(Func<string, bool> predicate)
{
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
return
GetEmbededResourceNames<TAssembly>()
.Where(predicate)
.Select(name => ReadEmbededResource(typeof(TAssembly), name))
.Where(x => !string.IsNullOrEmpty(x));
}
public static IEnumerable<string> GetEmbededResourceNames<TAssembly>()
{
var assembly = Assembly.GetAssembly(typeof(TAssembly));
return assembly.GetManifestResourceNames();
}
public static string ReadEmbededResource<TAssembly, TNamespace>(string name)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
return ReadEmbededResource(typeof(TAssembly), typeof(TNamespace), name);
}
public static string ReadEmbededResource(Type assemblyType, Type namespaceType, string name)
{
if (assemblyType == null) throw new ArgumentNullException(nameof(assemblyType));
if (namespaceType == null) throw new ArgumentNullException(nameof(namespaceType));
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
return ReadEmbededResource(assemblyType, $"{namespaceType.Namespace}.{name}");
}
public static string ReadEmbededResource(Type assemblyType, string name)
{
if (assemblyType == null) throw new ArgumentNullException(nameof(assemblyType));
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
var assembly = Assembly.GetAssembly(assemblyType);
using (var resourceStream = assembly.GetManifestResourceStream(name))
{
if (resourceStream == null) return null;
using (var streamReader = new StreamReader(resourceStream))
{
return streamReader.ReadToEnd();
}
}
}
}
I know this is old, but I just wanted to point out for NETMF (.Net MicroFramework), you can easily do this:
string response = Resources.GetString(Resources.StringResources.MyFileName);
Since NETMF doesn't have GetManifestResourceStream
I read an embedded resource text file use:
/// <summary>
/// Converts to generic list a byte array
/// </summary>
/// <param name="content">byte array (embedded resource)</param>
/// <returns>generic list of strings</returns>
private List<string> GetLines(byte[] content)
{
string s = Encoding.Default.GetString(content, 0, content.Length - 1);
return new List<string>(s.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
}
Sample:
var template = GetLines(Properties.Resources.LasTemplate /* resource name */);
template.ForEach(ln =>
{
Debug.WriteLine(ln);
});
Some VS .NET project types don’t auto-generate a .NET (.resx) file. The following steps add a Resource file to your project:
Right-click the project node and select Add/New Item, scroll to Resources File. In the Name box choose an appropriate name, for instance Resources and click the button Add.
The resource file Resources.resx is added to the project and can be seen as a node in the solution explorer.
Actually, two files are created, there is also an auto-generated C# class Resources.Designer.cs. Don’t edit it, it is maintained by VS. The file contains a class named Resources.
Now you can add a text file as a resource, for example an xml file:
Double-click Resources.resx. Select Add Resource > Add Existing File and scroll to the file you want to be included. Leave the default value Internal for Access Modify.
An icon represents the new resource item. If selected, the property pane shows its properties. For xml files, under the property Encoding select Unicode (UTF-8) – Codepage 65001 instead of the default local codepage. For other text files select the correct encoding of this file, for example codepage 1252.
For text files like xml files, the class Resources has a property of type string that is named after the included file. If the file name is e.g. RibbonManifest.xml, then the property should have the name RibbonManifest. You find the exact name in the code file Resources.Designer.cs.
Use the string property like any other string property, for example: string xml = Resources.RibbonManifest. The general form is ResourceFileName.IncludedTextFileName. Don’t use ResourceManager.GetString since the get-function of the string property has done that already.
This is a class which you might find very convenient for reading embedded resource files from the current Assembly:
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
public static class EmbeddedResourceUtils
{
public static string ReadFromResourceFile(string endingFileName)
{
var assembly = Assembly.GetExecutingAssembly();
var manifestResourceNames = assembly.GetManifestResourceNames();
foreach (var resourceName in manifestResourceNames)
{
var fileNameFromResourceName = _GetFileNameFromResourceName(resourceName);
if (!fileNameFromResourceName.EndsWith(endingFileName))
{
continue;
}
using (var manifestResourceStream = assembly.GetManifestResourceStream(resourceName))
{
if (manifestResourceStream == null)
{
continue;
}
using (var streamReader = new StreamReader(manifestResourceStream))
{
return streamReader.ReadToEnd();
}
}
}
return null;
}
// https://stackoverflow.com/a/32176198/3764804
private static string _GetFileNameFromResourceName(string resourceName)
{
var stringBuilder = new StringBuilder();
var escapeDot = false;
var haveExtension = false;
for (var resourceNameIndex = resourceName.Length - 1;
resourceNameIndex >= 0;
resourceNameIndex--)
{
if (resourceName[resourceNameIndex] == '_')
{
escapeDot = true;
continue;
}
if (resourceName[resourceNameIndex] == '.')
{
if (!escapeDot)
{
if (haveExtension)
{
stringBuilder.Append('\\');
continue;
}
haveExtension = true;
}
}
else
{
escapeDot = false;
}
stringBuilder.Append(resourceName[resourceNameIndex]);
}
var fileName = Path.GetDirectoryName(stringBuilder.ToString());
return fileName == null ? null : new string(fileName.Reverse().ToArray());
}
}
After reading all the solutions posted here. This is how I solved it:
// How to embedded a "Text file" inside of a C# project
// and read it as a resource from c# code:
//
// (1) Add Text File to Project. example: 'myfile.txt'
//
// (2) Change Text File Properties:
// Build-action: EmbeddedResource
// Logical-name: myfile.txt
// (note only 1 dot permitted in filename)
//
// (3) from c# get the string for the entire embedded file as follows:
//
// string myfile = GetEmbeddedResourceFile("myfile.txt");
public static string GetEmbeddedResourceFile(string filename) {
var a = System.Reflection.Assembly.GetExecutingAssembly();
using (var s = a.GetManifestResourceStream(filename))
using (var r = new System.IO.StreamReader(s))
{
string result = r.ReadToEnd();
return result;
}
return "";
}
The answer is quite simple, simply do this if you added the file directly from the resources.resx.
string textInResourceFile = fileNameSpace.Properties.Resources.fileName;
With that line of code, the text from the file is directly read from the file and put into the string variable.
I wanted to read the embedded resource just as a byte array (without assuming any specific encoding), and I ended up using a MemoryStream which makes it very simple:
using var resStream = assembly.GetManifestResourceStream(GetType(), "file.txt");
var ms = new MemoryStream();
await resStream .CopyToAsync(ms);
var bytes = ms.ToArray();
public class AssemblyTextFileReader
{
private readonly Assembly _assembly;
public AssemblyTextFileReader(Assembly assembly)
{
_assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));
}
public async Task<string> ReadFileAsync(string fileName)
{
var resourceName = _assembly.GetManifestResourceName(fileName);
using (var stream = _assembly.GetManifestResourceStream(resourceName))
{
using (var reader = new StreamReader(stream))
{
return await reader.ReadToEndAsync();
}
}
}
}
public static class AssemblyExtensions
{
public static string GetManifestResourceName(this Assembly assembly, string fileName)
{
string name = assembly.GetManifestResourceNames().SingleOrDefault(n => n.EndsWith(fileName, StringComparison.InvariantCultureIgnoreCase));
if (string.IsNullOrEmpty(name))
{
throw new FileNotFoundException($"Embedded file '{fileName}' could not be found in assembly '{assembly.FullName}'.", fileName);
}
return name;
}
}
// To use the code above:
var reader = new AssemblyTextFileReader(assembly);
string text = await reader.ReadFileAsync(#"MyFile.txt");
I was annoyed that you had to always include the namespace and the folder in the string. I wanted to simplify the access to the embedded resources. This is why I wrote this little class. Feel free to use and improve!
Usage:
using(Stream stream = EmbeddedResources.ExecutingResources.GetStream("filename.txt"))
{
//...
}
Class:
public class EmbeddedResources
{
private static readonly Lazy<EmbeddedResources> _callingResources = new Lazy<EmbeddedResources>(() => new EmbeddedResources(Assembly.GetCallingAssembly()));
private static readonly Lazy<EmbeddedResources> _entryResources = new Lazy<EmbeddedResources>(() => new EmbeddedResources(Assembly.GetEntryAssembly()));
private static readonly Lazy<EmbeddedResources> _executingResources = new Lazy<EmbeddedResources>(() => new EmbeddedResources(Assembly.GetExecutingAssembly()));
private readonly Assembly _assembly;
private readonly string[] _resources;
public EmbeddedResources(Assembly assembly)
{
_assembly = assembly;
_resources = assembly.GetManifestResourceNames();
}
public static EmbeddedResources CallingResources => _callingResources.Value;
public static EmbeddedResources EntryResources => _entryResources.Value;
public static EmbeddedResources ExecutingResources => _executingResources.Value;
public Stream GetStream(string resName) => _assembly.GetManifestResourceStream(_resources.Single(s => s.Contains(resName)));
}
As indicated by SonarCloud better to do:
public class Example
{
public static void Main()
{
// Compliant: type of the current class
Assembly assembly = typeof(Example).Assembly;
Console.WriteLine("Assembly name: {0}", assem.FullName);
// Non-compliant
Assembly assembly = Assembly.GetExecutingAssembly();
Console.WriteLine("Assembly name: {0}", assem.FullName);
}
}
For all the people that just quickly want the text of a hardcoded file in winforms;
Right-click your application in the solution explorer > Resources > Add your file.
Click on it, and in the properties tab set the "FileType" to "Text".
In your program just do Resources.<name of resource>.toString(); to read the file.
I would not recommend this as best practice or anything, but it works quickly and does what it needs to do.
Read Embedded TXT FILE on Form Load Event.
Set the Variables Dynamically.
string f1 = "AppName.File1.Ext";
string f2 = "AppName.File2.Ext";
string f3 = "AppName.File3.Ext";
Call a Try Catch.
try
{
IncludeText(f1,f2,f3);
/// Pass the Resources Dynamically
/// through the call stack.
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
/// Error for if the Stream is Null.
}
Create Void for IncludeText(), Visual Studio Does this for you. Click the Lightbulb to AutoGenerate The CodeBlock.
Put the following inside the Generated Code Block
Resource 1
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(file1))
using (StreamReader reader = new StreamReader(stream))
{
string result1 = reader.ReadToEnd();
richTextBox1.AppendText(result1 + Environment.NewLine + Environment.NewLine );
}
Resource 2
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(file2))
using (StreamReader reader = new StreamReader(stream))
{
string result2 = reader.ReadToEnd();
richTextBox1.AppendText(
result2 + Environment.NewLine +
Environment.NewLine );
}
Resource 3
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(file3))
using (StreamReader reader = new StreamReader(stream))
{
string result3 = reader.ReadToEnd();
richTextBox1.AppendText(result3);
}
If you wish to send the returned variable somewhere else, just call another function and...
using (StreamReader reader = new StreamReader(stream))
{
string result3 = reader.ReadToEnd();
///richTextBox1.AppendText(result3);
string extVar = result3;
/// another try catch here.
try {
SendVariableToLocation(extVar)
{
//// Put Code Here.
}
}
catch (Exception ex)
{
Messagebox.Show(ex.Message);
}
}
What this achieved was this, a method to combine multiple txt files, and read their embedded data, inside a single rich text box. which was my desired effect with this sample of Code.
For users that are using VB.Net
Imports System.IO
Imports System.Reflection
Dim reader As StreamReader
Dim ass As Assembly = Assembly.GetExecutingAssembly()
Dim sFileName = "MyApplicationName.JavaScript.js"
Dim reader = New StreamReader(ass.GetManifestResourceStream(sFileName))
Dim sScriptText = reader.ReadToEnd()
reader.Close()
where MyApplicationName is namespace of my application.
It is not the assembly name.
This name is define in project's properties (Application tab).
If you don't find correct resource name, you can use GetManifestResourceNames() function
Dim resourceName() As String = ass.GetManifestResourceNames()
or
Dim sName As String
= ass.GetManifestResourceNames()
.Single(Function(x) x.EndsWith("JavaScript.js"))
or
Dim sNameList
= ass.GetManifestResourceNames()
.Where(Function(x As String) x.EndsWith(".js"))

How can I stream simple text file using

I'm trying to read a simple text file using reflection just as a learning case. I'm not getting an error, but I'm also not getting the desired result of "hello world". The variable stream is coming back null.
string output = "";
var asm = Assembly.GetExecutingAssembly();
using (var stream = asm.GetManifestResourceStream("ConsoleApp1.data1.txt"))
{
if (stream != null)
{
var reader = new StreamReader(stream);
output = reader.ReadToEnd();
Console.WriteLine(output);
}
}
You're reading from a manifest resource, which means the text file needs to be embedded in the dll. Right click on the file and choose Properties, then set the Build Action to "Embedded Resource".

How can I read Persian line in csv file c#

I want to read a simple CSV file with comma separated with this code:
var reader = new StreamReader(File.OpenRead(#"d:\34.csv"));
List<string> listA = new List<string>();
List<string> listB = new List<string>();
while (!reader.EndOfStream)
{
var line = reader.ReadLine();
var values = line.Split(',');
listA.Add(values[0]);
listB.Add(values[1]);
}
MessageBox.Show("READ IT!!!");
But when I read the file an debug that code,attention can not read Persian or Arabic character! How can I solve that? I think my file is not valid encoding?
if your CSV file contains just one line the ReadToEnd could be acceptable, but if you have a log file composed by more than one line then it is better to read line by line using ReadLine of the StreamReader object
link for true answer and more information
using (StreamReader sr = new StreamReader("c:/temp/34.csv"))
{
string currentLine;
// currentLine will be null when the StreamReader reaches the end of file
while((currentLine = sr.ReadLine()) != null)
{
// Search, case insensitive, if the currentLine contains the searched keyword
if(currentLine.IndexOf("I/RPTGEN", StringComparison.CurrentCultureIgnoreCase) >= 0)
{
Console.WriteLine(currentLine);
}
}
}
More information
You can create a class composed of get and set for each line of the CSV . You can then instantiate an object list to retrieve the CSV lines.
Try this way :
class Program
{
static void Main(string[] args)
{
var reader = new StreamReader(File.OpenRead(#"YourCSV"),Encoding.Unicode);
List<Customer> customer = new List<Customer>();
while (!reader.EndOfStream)
{
Customer c = new Customer
{
m_line1 = null,
m_line2 = null,
};
var line = reader.ReadLine();
var tokens = line.Split(',');
c.m_line1 = tokens[0];
c.m_line2 = tokens[1];
customer.Add(c);
}
foreach(var s in customer)
{
Console.Writline(s);
Console.Readline();
}
}
}
class Customer
{
private string line1;
public string m_line1
{
get
{
return line1;
}
set
{
line1= value;
}
}
private string line2;
public string m_line2
{
get
{
return line2;
}
set
{
line2= value;
}
}
You will have to pass the character encoding to the StreamReader constructor. There is no such thing as plain text. Reading text requires knowing its encoding.
The line
using (StreamReader sr = new StreamReader("c:/temp/34.csv"))
should be
using (StreamReader sr = new StreamReader("c:/temp/34.csv"), myencoding)
what myencoding is is something only you can know. With what encoding was the file saved? That's the encoding you need there. If the file was generated on Windows, and educated guess of the most likely encoding would be it is UTF-16LE. That encoding is available as Encoding.Unicode - which is a bad name, it should have been Encoding.UTF16LE, but that's the name the .NET framework uses.
Other possible encodings that are supported by StreamReader are listed on https://msdn.microsoft.com/en-us/library/System.Text.Encoding_properties(v=vs.110).aspx
If you don't know with what encoding the file was saved, some encodings leave hints in the form of a Byte order mark sometimes abbreviated to BOM. A byte order mark are the first few bytes of a text document that tell you its encoding. You can find more information on the byte order mark, and some of its values on http://en.wikipedia.org/wiki/Byte_order_mark
Relying on the BOM is generally a bad idea, because
it's not a full-proof solution: some encodings don't use a BOM, or make the BOM optional
Even if you successfully determine the encoding, that doesn't mean that StreamReader knows how to handle that encoding (though this is unlikely, but possible)
the BOM might not be a BOM at all, but be part of the actual text (also unlikely but possible)
In some cases it is impossible to know the encoding of a file, notably if the file comes from a file upload on the web, or if someone just mailed you the file, and they don't know how they encoded it. This can be a good reason not to allow "plain text" uploads (which is reasonable because, it can do with a little repetition, there is no such thing as plain text).
tl;dr: The most likely thing to work is one of
using (StreamReader sr = new StreamReader(File.OpenRead(#"c:/temp/34.csv"),Encoding.Unicode) {
...
}
or
using (StreamReader sr = new StreamReader(File.OpenRead(#"c:/temp/34.csv"),Encoding.UTF8)
or
using (StreamReader sr = new StreamReader(File.OpenRead(#"c:/temp/34.csv"),Encoding.UTF32)

Null reference exception while iterating over a list of StreamReader objects

I am making a simple Program which searches for a particular Name in a set of files.I have around 23 files to go through. To achieve this I am using StreamReader class, therefore, to write less of code, I have made a
List<StreamReader> FileList = new List<StreamReader>();
list containing elements of type StreamReader and my plan is to iterate over the list and open each file:
foreach(StreamReader Element in FileList)
{
while (!Element.EndOfStream)
{
// Code to process the file here.
}
}
I have opened all of the streams in the FileList.The problem is that I am getting a
Null Reference Exception
at the condition in the while loop.
Can anybody tell me what mistake I am doing here and why I am getting this exception and what are the steps I can take to correct this problem?
As peoples described on above, Use the following way:
using (StreamReader sr = new StreamReader("filename.txt"))
{
...
}
If you're trying to store files with their names on a list, I suggest you to use a dictionary:
Dictionary<string, string> Files = new Dictionary<string, string>();
using (StreamReader sr = new StreamReader("filename.txt"))
{
string total = "";
string line;
while ((line = sr.ReadLine()) != null)
{
total += line;
}
Files.Add("filename.txt", line);
}
To access them:
Console.WriteLine("Filename.txt has: " + Files["filename.txt"]);
Or If you want to get the StreamReader It self not the file text, You can use:
Dictionary<string, StreamReader> Files = new Dictionary<string, StreamReader>();
using (StreamReader sr = new StreamReader("filename.txt"))
{
Files.Add("filename.txt", sr);
}

Access text files contained in a solution [duplicate]

How do I read an embedded resource (text file) using StreamReader and return it as a string? My current script uses a Windows form and textbox that allows the user to find and replace text in a text file that is not embedded.
private void button1_Click(object sender, EventArgs e)
{
StringCollection strValuesToSearch = new StringCollection();
strValuesToSearch.Add("Apple");
string stringToReplace;
stringToReplace = textBox1.Text;
StreamReader FileReader = new StreamReader(#"C:\MyFile.txt");
string FileContents;
FileContents = FileReader.ReadToEnd();
FileReader.Close();
foreach (string s in strValuesToSearch)
{
if (FileContents.Contains(s))
FileContents = FileContents.Replace(s, stringToReplace);
}
StreamWriter FileWriter = new StreamWriter(#"MyFile.txt");
FileWriter.Write(FileContents);
FileWriter.Close();
}
You can use the Assembly.GetManifestResourceStream Method:
Add the following usings
using System.IO;
using System.Reflection;
Set property of relevant file:
Parameter Build Action with value Embedded Resource
Use the following code
var assembly = Assembly.GetExecutingAssembly();
var resourceName = "MyCompany.MyProduct.MyFile.txt";
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
string result = reader.ReadToEnd();
}
resourceName is the name of one of the resources embedded in assembly.
For example, if you embed a text file named "MyFile.txt" that is placed in the root of a project with default namespace "MyCompany.MyProduct", then resourceName is "MyCompany.MyProduct.MyFile.txt".
You can get a list of all resources in an assembly using the Assembly.GetManifestResourceNames Method.
A no brainer astute to get the resourceName from the file name only (by pass the namespace stuff):
string resourceName = assembly.GetManifestResourceNames()
.Single(str => str.EndsWith("YourFileName.txt"));
A complete example:
public string ReadResource(string name)
{
// Determine path
var assembly = Assembly.GetExecutingAssembly();
string resourcePath = name;
// Format: "{Namespace}.{Folder}.{filename}.{Extension}"
if (!name.StartsWith(nameof(SignificantDrawerCompiler)))
{
resourcePath = assembly.GetManifestResourceNames()
.Single(str => str.EndsWith(name));
}
using (Stream stream = assembly.GetManifestResourceStream(resourcePath))
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
or as an async extension method:
internal static class AssemblyExtensions
{
public static async Task<string> ReadResourceAsync(this Assembly assembly, string name)
{
// Determine path
string resourcePath = name;
// Format: "{Namespace}.{Folder}.{filename}.{Extension}"
if (!name.StartsWith(nameof(SignificantDrawerCompiler)))
{
resourcePath = assembly.GetManifestResourceNames()
.Single(str => str.EndsWith(name));
}
using Stream stream = assembly.GetManifestResourceStream(resourcePath)!;
using StreamReader reader = new(stream);
return await reader.ReadToEndAsync();
}
}
// Usage
string resourceText = await Assembly.GetExecutingAssembly().ReadResourceAsync("myResourceName");
You can add a file as a resource using two separate methods.
The C# code required to access the file is different, depending on the method used to add the file in the first place.
Method 1: Add existing file, set property to Embedded Resource
Add the file to your project, then set the type to Embedded Resource.
NOTE: If you add the file using this method, you can use GetManifestResourceStream to access it (see answer from #dtb).
Method 2: Add file to Resources.resx
Open up the Resources.resx file, use the dropdown box to add the file, set Access Modifier to public.
NOTE: If you add the file using this method, you can use Properties.Resources to access it (see answer from #Night Walker).
Basically, you use System.Reflection to get a reference to the current Assembly. Then, you use GetManifestResourceStream().
Example, from the page I posted:
Note: need to add using System.Reflection; for this to work
Assembly _assembly;
StreamReader _textStreamReader;
try
{
_assembly = Assembly.GetExecutingAssembly();
_textStreamReader = new StreamReader(_assembly.GetManifestResourceStream("MyNamespace.MyTextFile.txt"));
}
catch
{
MessageBox.Show("Error accessing resources!");
}
In Visual Studio you can directly embed access to a file resource via the Resources tab of the Project properties ("Analytics" in this example).
The resulting file can then be accessed as a byte array by
byte[] jsonSecrets = GoogleAnalyticsExtractor.Properties.Resources.client_secrets_reporter;
Should you need it as a stream, then ( from https://stackoverflow.com/a/4736185/432976 )
Stream stream = new MemoryStream(jsonSecrets)
When you added the file to the resources, you should select its Access Modifiers as public than you can make something like following.
byte[] clistAsByteArray = Properties.Resources.CLIST01;
CLIST01 is the name of the embedded file.
Actually you can go to the resources.Designer.cs and see what is the name of the getter.
adding e.g. Testfile.sql
Project Menu -> Properties -> Resources -> Add Existing file
string queryFromResourceFile = Properties.Resources.Testfile.ToString();
I know it is an old thread, but this is what worked for me :
add the text file to the project resources
set the access modifier to public, as showed above by Andrew Hill
read the text like this :
textBox1 = new TextBox();
textBox1.Text = Properties.Resources.SomeText;
The text that I added to the resources: 'SomeText.txt'
Something I learned just now is that your file is not allowed to have a "." (dot) in the filename.
Templates.plainEmailBodyTemplate-en.txt --> Works!!!
Templates.plainEmailBodyTemplate.en.txt --> doesn't work via GetManifestResourceStream()
Probably because the framework gets confused over namespaces vs filename...
You can also use this simplified version of #dtb's answer:
public string GetEmbeddedResource(string ns, string res)
{
using (var reader = new StreamReader(Assembly.GetExecutingAssembly().GetManifestResourceStream(string.Format("{0}.{1}", ns, res))))
{
return reader.ReadToEnd();
}
}
By all your powers combined I use this helper class for reading resources from any assembly and any namespace in a generic way.
public class ResourceReader
{
public static IEnumerable<string> FindEmbededResources<TAssembly>(Func<string, bool> predicate)
{
if (predicate == null) throw new ArgumentNullException(nameof(predicate));
return
GetEmbededResourceNames<TAssembly>()
.Where(predicate)
.Select(name => ReadEmbededResource(typeof(TAssembly), name))
.Where(x => !string.IsNullOrEmpty(x));
}
public static IEnumerable<string> GetEmbededResourceNames<TAssembly>()
{
var assembly = Assembly.GetAssembly(typeof(TAssembly));
return assembly.GetManifestResourceNames();
}
public static string ReadEmbededResource<TAssembly, TNamespace>(string name)
{
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
return ReadEmbededResource(typeof(TAssembly), typeof(TNamespace), name);
}
public static string ReadEmbededResource(Type assemblyType, Type namespaceType, string name)
{
if (assemblyType == null) throw new ArgumentNullException(nameof(assemblyType));
if (namespaceType == null) throw new ArgumentNullException(nameof(namespaceType));
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
return ReadEmbededResource(assemblyType, $"{namespaceType.Namespace}.{name}");
}
public static string ReadEmbededResource(Type assemblyType, string name)
{
if (assemblyType == null) throw new ArgumentNullException(nameof(assemblyType));
if (string.IsNullOrEmpty(name)) throw new ArgumentNullException(nameof(name));
var assembly = Assembly.GetAssembly(assemblyType);
using (var resourceStream = assembly.GetManifestResourceStream(name))
{
if (resourceStream == null) return null;
using (var streamReader = new StreamReader(resourceStream))
{
return streamReader.ReadToEnd();
}
}
}
}
I know this is old, but I just wanted to point out for NETMF (.Net MicroFramework), you can easily do this:
string response = Resources.GetString(Resources.StringResources.MyFileName);
Since NETMF doesn't have GetManifestResourceStream
I read an embedded resource text file use:
/// <summary>
/// Converts to generic list a byte array
/// </summary>
/// <param name="content">byte array (embedded resource)</param>
/// <returns>generic list of strings</returns>
private List<string> GetLines(byte[] content)
{
string s = Encoding.Default.GetString(content, 0, content.Length - 1);
return new List<string>(s.Split(new[] { Environment.NewLine }, StringSplitOptions.None));
}
Sample:
var template = GetLines(Properties.Resources.LasTemplate /* resource name */);
template.ForEach(ln =>
{
Debug.WriteLine(ln);
});
Some VS .NET project types don’t auto-generate a .NET (.resx) file. The following steps add a Resource file to your project:
Right-click the project node and select Add/New Item, scroll to Resources File. In the Name box choose an appropriate name, for instance Resources and click the button Add.
The resource file Resources.resx is added to the project and can be seen as a node in the solution explorer.
Actually, two files are created, there is also an auto-generated C# class Resources.Designer.cs. Don’t edit it, it is maintained by VS. The file contains a class named Resources.
Now you can add a text file as a resource, for example an xml file:
Double-click Resources.resx. Select Add Resource > Add Existing File and scroll to the file you want to be included. Leave the default value Internal for Access Modify.
An icon represents the new resource item. If selected, the property pane shows its properties. For xml files, under the property Encoding select Unicode (UTF-8) – Codepage 65001 instead of the default local codepage. For other text files select the correct encoding of this file, for example codepage 1252.
For text files like xml files, the class Resources has a property of type string that is named after the included file. If the file name is e.g. RibbonManifest.xml, then the property should have the name RibbonManifest. You find the exact name in the code file Resources.Designer.cs.
Use the string property like any other string property, for example: string xml = Resources.RibbonManifest. The general form is ResourceFileName.IncludedTextFileName. Don’t use ResourceManager.GetString since the get-function of the string property has done that already.
This is a class which you might find very convenient for reading embedded resource files from the current Assembly:
using System.IO;
using System.Linq;
using System.Text;
using System.Reflection;
public static class EmbeddedResourceUtils
{
public static string ReadFromResourceFile(string endingFileName)
{
var assembly = Assembly.GetExecutingAssembly();
var manifestResourceNames = assembly.GetManifestResourceNames();
foreach (var resourceName in manifestResourceNames)
{
var fileNameFromResourceName = _GetFileNameFromResourceName(resourceName);
if (!fileNameFromResourceName.EndsWith(endingFileName))
{
continue;
}
using (var manifestResourceStream = assembly.GetManifestResourceStream(resourceName))
{
if (manifestResourceStream == null)
{
continue;
}
using (var streamReader = new StreamReader(manifestResourceStream))
{
return streamReader.ReadToEnd();
}
}
}
return null;
}
// https://stackoverflow.com/a/32176198/3764804
private static string _GetFileNameFromResourceName(string resourceName)
{
var stringBuilder = new StringBuilder();
var escapeDot = false;
var haveExtension = false;
for (var resourceNameIndex = resourceName.Length - 1;
resourceNameIndex >= 0;
resourceNameIndex--)
{
if (resourceName[resourceNameIndex] == '_')
{
escapeDot = true;
continue;
}
if (resourceName[resourceNameIndex] == '.')
{
if (!escapeDot)
{
if (haveExtension)
{
stringBuilder.Append('\\');
continue;
}
haveExtension = true;
}
}
else
{
escapeDot = false;
}
stringBuilder.Append(resourceName[resourceNameIndex]);
}
var fileName = Path.GetDirectoryName(stringBuilder.ToString());
return fileName == null ? null : new string(fileName.Reverse().ToArray());
}
}
After reading all the solutions posted here. This is how I solved it:
// How to embedded a "Text file" inside of a C# project
// and read it as a resource from c# code:
//
// (1) Add Text File to Project. example: 'myfile.txt'
//
// (2) Change Text File Properties:
// Build-action: EmbeddedResource
// Logical-name: myfile.txt
// (note only 1 dot permitted in filename)
//
// (3) from c# get the string for the entire embedded file as follows:
//
// string myfile = GetEmbeddedResourceFile("myfile.txt");
public static string GetEmbeddedResourceFile(string filename) {
var a = System.Reflection.Assembly.GetExecutingAssembly();
using (var s = a.GetManifestResourceStream(filename))
using (var r = new System.IO.StreamReader(s))
{
string result = r.ReadToEnd();
return result;
}
return "";
}
The answer is quite simple, simply do this if you added the file directly from the resources.resx.
string textInResourceFile = fileNameSpace.Properties.Resources.fileName;
With that line of code, the text from the file is directly read from the file and put into the string variable.
I wanted to read the embedded resource just as a byte array (without assuming any specific encoding), and I ended up using a MemoryStream which makes it very simple:
using var resStream = assembly.GetManifestResourceStream(GetType(), "file.txt");
var ms = new MemoryStream();
await resStream .CopyToAsync(ms);
var bytes = ms.ToArray();
public class AssemblyTextFileReader
{
private readonly Assembly _assembly;
public AssemblyTextFileReader(Assembly assembly)
{
_assembly = assembly ?? throw new ArgumentNullException(nameof(assembly));
}
public async Task<string> ReadFileAsync(string fileName)
{
var resourceName = _assembly.GetManifestResourceName(fileName);
using (var stream = _assembly.GetManifestResourceStream(resourceName))
{
using (var reader = new StreamReader(stream))
{
return await reader.ReadToEndAsync();
}
}
}
}
public static class AssemblyExtensions
{
public static string GetManifestResourceName(this Assembly assembly, string fileName)
{
string name = assembly.GetManifestResourceNames().SingleOrDefault(n => n.EndsWith(fileName, StringComparison.InvariantCultureIgnoreCase));
if (string.IsNullOrEmpty(name))
{
throw new FileNotFoundException($"Embedded file '{fileName}' could not be found in assembly '{assembly.FullName}'.", fileName);
}
return name;
}
}
// To use the code above:
var reader = new AssemblyTextFileReader(assembly);
string text = await reader.ReadFileAsync(#"MyFile.txt");
I was annoyed that you had to always include the namespace and the folder in the string. I wanted to simplify the access to the embedded resources. This is why I wrote this little class. Feel free to use and improve!
Usage:
using(Stream stream = EmbeddedResources.ExecutingResources.GetStream("filename.txt"))
{
//...
}
Class:
public class EmbeddedResources
{
private static readonly Lazy<EmbeddedResources> _callingResources = new Lazy<EmbeddedResources>(() => new EmbeddedResources(Assembly.GetCallingAssembly()));
private static readonly Lazy<EmbeddedResources> _entryResources = new Lazy<EmbeddedResources>(() => new EmbeddedResources(Assembly.GetEntryAssembly()));
private static readonly Lazy<EmbeddedResources> _executingResources = new Lazy<EmbeddedResources>(() => new EmbeddedResources(Assembly.GetExecutingAssembly()));
private readonly Assembly _assembly;
private readonly string[] _resources;
public EmbeddedResources(Assembly assembly)
{
_assembly = assembly;
_resources = assembly.GetManifestResourceNames();
}
public static EmbeddedResources CallingResources => _callingResources.Value;
public static EmbeddedResources EntryResources => _entryResources.Value;
public static EmbeddedResources ExecutingResources => _executingResources.Value;
public Stream GetStream(string resName) => _assembly.GetManifestResourceStream(_resources.Single(s => s.Contains(resName)));
}
As indicated by SonarCloud better to do:
public class Example
{
public static void Main()
{
// Compliant: type of the current class
Assembly assembly = typeof(Example).Assembly;
Console.WriteLine("Assembly name: {0}", assem.FullName);
// Non-compliant
Assembly assembly = Assembly.GetExecutingAssembly();
Console.WriteLine("Assembly name: {0}", assem.FullName);
}
}
For all the people that just quickly want the text of a hardcoded file in winforms;
Right-click your application in the solution explorer > Resources > Add your file.
Click on it, and in the properties tab set the "FileType" to "Text".
In your program just do Resources.<name of resource>.toString(); to read the file.
I would not recommend this as best practice or anything, but it works quickly and does what it needs to do.
Read Embedded TXT FILE on Form Load Event.
Set the Variables Dynamically.
string f1 = "AppName.File1.Ext";
string f2 = "AppName.File2.Ext";
string f3 = "AppName.File3.Ext";
Call a Try Catch.
try
{
IncludeText(f1,f2,f3);
/// Pass the Resources Dynamically
/// through the call stack.
}
catch (Exception Ex)
{
MessageBox.Show(Ex.Message);
/// Error for if the Stream is Null.
}
Create Void for IncludeText(), Visual Studio Does this for you. Click the Lightbulb to AutoGenerate The CodeBlock.
Put the following inside the Generated Code Block
Resource 1
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(file1))
using (StreamReader reader = new StreamReader(stream))
{
string result1 = reader.ReadToEnd();
richTextBox1.AppendText(result1 + Environment.NewLine + Environment.NewLine );
}
Resource 2
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(file2))
using (StreamReader reader = new StreamReader(stream))
{
string result2 = reader.ReadToEnd();
richTextBox1.AppendText(
result2 + Environment.NewLine +
Environment.NewLine );
}
Resource 3
var assembly = Assembly.GetExecutingAssembly();
using (Stream stream = assembly.GetManifestResourceStream(file3))
using (StreamReader reader = new StreamReader(stream))
{
string result3 = reader.ReadToEnd();
richTextBox1.AppendText(result3);
}
If you wish to send the returned variable somewhere else, just call another function and...
using (StreamReader reader = new StreamReader(stream))
{
string result3 = reader.ReadToEnd();
///richTextBox1.AppendText(result3);
string extVar = result3;
/// another try catch here.
try {
SendVariableToLocation(extVar)
{
//// Put Code Here.
}
}
catch (Exception ex)
{
Messagebox.Show(ex.Message);
}
}
What this achieved was this, a method to combine multiple txt files, and read their embedded data, inside a single rich text box. which was my desired effect with this sample of Code.
For users that are using VB.Net
Imports System.IO
Imports System.Reflection
Dim reader As StreamReader
Dim ass As Assembly = Assembly.GetExecutingAssembly()
Dim sFileName = "MyApplicationName.JavaScript.js"
Dim reader = New StreamReader(ass.GetManifestResourceStream(sFileName))
Dim sScriptText = reader.ReadToEnd()
reader.Close()
where MyApplicationName is namespace of my application.
It is not the assembly name.
This name is define in project's properties (Application tab).
If you don't find correct resource name, you can use GetManifestResourceNames() function
Dim resourceName() As String = ass.GetManifestResourceNames()
or
Dim sName As String
= ass.GetManifestResourceNames()
.Single(Function(x) x.EndsWith("JavaScript.js"))
or
Dim sNameList
= ass.GetManifestResourceNames()
.Where(Function(x As String) x.EndsWith(".js"))

Categories

Resources