0

From comments I now know that .cs file get compiled and are not really in the exe, so this post is on how to get the text from an txt content

------------------------------------------------------------------------

For example if I had a project that has a file named "ThisIsText.txt" inside it, could I be able to get the text from it? What about if it's a .cs C# file?

I know that you could get text with System.IO.File.ReadAllText, but that only works if the file exist, not when the program is compiled.

For example in img, could a C# console program just print out the Program.cs or ThisIsText.txt? enter image description here

Edit: The txt file build action is Content.

Michael
  • 395
  • 1
  • 13
  • How does it have the file inside it? – ProgrammingLlama Jun 05 '20 at 06:48
  • How can one have value at compile time? – Vivek Nuna Jun 05 '20 at 06:48
  • @John In visual studio you could add txt or other format of file into the project – Michael Jun 05 '20 at 06:52
  • Does this answer your question? [How to read embedded resource text file](https://stackoverflow.com/questions/3314140/how-to-read-embedded-resource-text-file) – Sinatr Jun 05 '20 at 06:54
  • 1
    "What about if it's a .cs C# file" ...these get compiled, the raw .cs files are never added to the compiled executable. Are you really asking whether your application could be decompiled, so that someone else would be able to read the code or settings? The answer to that is, to some extent yes, .NET programs can be decompiled. You may not get out exactly what was put in, but it's possible that someone could understand some or all of it. People sometimes use obfuscation tools to try and minimise the risk of this. – ADyson Jun 05 '20 at 06:54
  • @Michael In Visual Studio you can add a file to a project and have it not be packaged with the project. You can also have a file in the project and set it as an embedded question. You can also add a Resources file to the project, and add things into that. Hence my question. – ProgrammingLlama Jun 05 '20 at 06:55
  • Should the cs file be compiled or is it just some content added ? – Cid Jun 05 '20 at 07:00
  • @Cid if it's the cs file then it should be compiled, if it's the text file then it's just content added – Michael Jun 05 '20 at 07:09
  • @John The file is just added under project, so it's not a resource file to the project, could the update in the post help clarify the question? – Michael Jun 05 '20 at 07:11
  • 3
    OK, you've shown us a text file in a project. Now what is its build action? Content? Embedded resource? What? Note that if is just a file in the project (build action "none") then it won't be included in the compiled executable. – ProgrammingLlama Jun 05 '20 at 07:11
  • 1
    @John, Ahhh....now I'm getting the problem. Thanks, let me do a little googleing – Michael Jun 05 '20 at 07:15
  • Are you looking for a Resource file? https://stackoverflow.com/questions/13194293/getting-a-string-dynamically-from-strings-resources – Robin Bennett Jun 05 '20 at 07:43
  • @John Yeah, it's Content, you could post an answer and I'll label it as the solution – Michael Jun 05 '20 at 07:48

0 Answers0