How do I read the contents of an embedded text file? [duplicate] - c#

This question already has answers here:
How to read embedded resource text file
(23 answers)
Closed 9 years ago.
I have a text file data.txt that's embedded in my solution (as described in this SO question).
How do I read the contents of this file as a string? I'm imagining something like this:
string data = Resources["data.txt"];
but that's not the way to do it.

If you add the file as a resource you should be able to access it like this:
Properties.Resources.data
Or alternatively if you set the Copy to Output Directory property to Copy always/Copy if newer, you can do something like:
using (FileStream fs = System.IO.File.Open("Resources/data.txt", FileMode.Open))
{
// do amazing stuff here ...
}

Related

How to get the contents of a .txt file into a string C# [duplicate]

This question already has answers here:
Reading From a Text File in C#
(8 answers)
Closed 8 months ago.
I have the file path of a txt file inside a string. How do I put the text of this file into another string?
Use File.ReadAllText
var path = "[yourPathHere]";
var contents = File.ReadAllText(path);

Reading JSON from FileStream [duplicate]

This question already has answers here:
C# using streams
(7 answers)
Easiest way to read from and write to files
(14 answers)
Closed 1 year ago.
In Xamarin.Forms project I have JSON file saved as Embedded Resource. I get it as FileStream by calling:
var assembly = typeof(App).GetTypeInfo().Assembly;
FileStream fileStream = assembly.GetFiles()[0];
How do I parse this stream to json string?
I know it is most likely super dumb question but I am confused, all I achieved was some random characters.
First, get the raw string from the JSON file by using
string jsonText = File.ReadAllText("/path/to/file.txt")
From there, you can convert the JSON string into an object (if needed):
MyClass jsonObj = JsonSerializer.Deserialize<MyClass>(jsonText);
Don't forget to import System.Text.Json.Serialization, System.Text.Json, and System.IO when working with JSON files.
You're having errors because you're trying to get a literal FileStream, which is not the intended use for it. You can read more about serializing JSONs here: https://learn.microsoft.com/en-us/dotnet/standard/serialization/system-text-json-how-to?pivots=dotnet-5-0

How to use Filestream with embedded resource file? [duplicate]

This question already has answers here:
How to read embedded resource text file
(23 answers)
C# Get full path of embedded ressource? [duplicate]
(2 answers)
Closed 3 years ago.
I have a code that opens file using filestream as below:-
using (var stream = new FileStream("abc.txt", FileMode.Open, FileAccess.ReadWrite))
I do not want to have it in any any directory rather I want it in embedded resource. I just copied the file in solution explorer and changed build action to embedded resource, now what should I do next to get it worked and achieve similar code behavior as above!
Thanks

Opening A File With Code [duplicate]

This question already has answers here:
Open file with associated application
(3 answers)
Closed 5 years ago.
I have a string of the file path and I want to open the file. Not in the way that it shows bytes in the console for example, but to open the original file with the program that suits him.
For example if my path shows a .docs file I want it to be opened with Word.
Any suggestions?
System.Diagnostics.Process.Start("CMD.exe",filePath);

Is FileInfo.Exists/Copy faster than File? [duplicate]

This question already has answers here:
What is the difference between File and FileInfo in C#?
(10 answers)
Closed 9 years ago.
As the title says is which one of the scenarios below is faster?
// Using FileInfo
FileInfo file = new FileInfo(#"C:\Test.txt");
if (file.Exists)
file.CopyTo(#"C:\TestCopy.txt");
// Using File
if (File.Exists(#"C:\Test.txt"))
File.Copy(#"C:\Test.txt", #"C:\TestCopy.txt");
I know the FileInfo is easier for the eye to read, but is one method faster than the other?
Difference is that FileInfo cache information: file existing check is executed once.
Then, if you check Exists property and THEN create file, new call to Exists property always will return false.

Categories