0

I am trying to read entire block from notepad on C# code. How do I make this work?

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            string[] lines = File.ReadAllLines(@"C:\Users\AA\Downloads\test2.txt");
            foreach (string line in lines)
            {
                Console.WriteLine(line);
            }
        }

content in text2.txt file is something like

panel1.Visible = false;
panel2.Visible = true;
panel3.Visible = false;
panel4.Visible = false;
panel5.Visible = false;
webBrowser1.Navigate("www.youtube.com");

If I make code like below and content in text.txt is just www.youtube.com, it read the line and execute but if I read entire block from notepad, it won't work. How do I make this work?

        private void pictureBox4_Click(object sender, EventArgs e)
        {
            panel1.Visible = false;
            panel2.Visible = true;
            panel3.Visible = false;
            panel4.Visible = false;
            panel5.Visible = false; 
            string readText = System.IO.File.ReadAllText(@"C:\Users\AA\Downloads\test.txt");
            webBrowser1.Navigate(readText);
        }

Thank you in advance. :(

John Krain
  • 29
  • 1
  • 3
  • 1
    So if I understand you want to execute code that is in a text file. C# is not a scripting language and can only execute compiled code. So you need to get the file content, compile it then you can execute it. – Martin Verjans Oct 12 '19 at 09:03
  • 1
    Not sure what you actually is trying to do, though when reading a text file, the read content will be just that, a text, and just because you wrote it in the same way as in your code, doesn't make it _execute_. For that you need a complete different setup, and that is way too broad to answer. – Asons Oct 12 '19 at 09:04
  • 1
    This could be a start: https://stackoverflow.com/questions/826398/is-it-possible-to-dynamically-compile-and-execute-c-sharp-code-fragments – Asons Oct 12 '19 at 09:06
  • Define 'execute' ! You will need to parse the lines and translate them to some predefined actions.. – TaW Oct 12 '19 at 09:13
  • I think you did not understand the purpose of a compiler. _Panel1.Visible = false;_ is part of the source code that get translated into machine instructions before it can be executed. You cannot just execute text. But you can read text from a file and use it as such like you did as you read the url from a file and passed this text to a browser call – Daniel Schmid Oct 12 '19 at 09:15
  • @MartinVerjans C# is a scripting language and this is really simple to do. – Filip Cordas Oct 12 '19 at 09:16
  • @FilipCordas so tell us how to use C# as a scripting language. I'm curious. Because it really isn't a scripting (interpreted) language. It is a compiled language – Daniel Schmid Oct 12 '19 at 09:20
  • @FilipCordas C# is a precompiled language, meaning the text you write is transformed by the compiler to MSIL code, which will be executed by the .Net framework runtime engine. Therefore, there is no way you can directly execute plain text, you need to precompile it first to MSIL code. And no this is not simple because compiling the text file will not make it run in the proper context and the variables OP is trying to change will not be accessible. – Martin Verjans Oct 12 '19 at 09:24
  • @MartinVerja Yes C# sharp will be compiled even when running it as a script so it's not a "true" scripting language but csx files have been around for years and there are a bunch of frameworks that prefer the script way of doing things. – Filip Cordas Oct 12 '19 at 09:30

1 Answers1

0

Lets start with a disclaimer. This is something you should probably not be doing because of security concerns and be sure you know what you are doing here.

On the answer Roslyn has introduced a scripting API a while ago. You can even execute with parameters like so

public class Globals
{
    public int X;
    public int Y;
}
var globals = new Globals { X = 1, Y = 2 };
Console.WriteLine(await CSharpScript.EvaluateAsync<int>("X+Y", globals: globals));

In your case the params will be panel1...5 and webBrowser1 but mind you might need to add libraries for C# forms. And might get weird errors because of the scope. If you really want to do this you should investigate the Roslyn api's because you do need to know how they work to use them to not make mistakes with this.

Filip Cordas
  • 2,531
  • 1
  • 12
  • 23
  • 1
    Indeed your answer is a good start, but as you mentioned should be handled very carefully. It looks like the scripting is running on another thread, which means will generate cross-thread exceptions when used with Windows.Forms. This is why this question might be considered way too broad, and also looks like a [XY Problem](https://meta.stackexchange.com/a/66378/319999) – Martin Verjans Oct 12 '19 at 09:35
  • @MartinVerjans Probably it's not something he really needs to do. I posted this answer because people are not familiar with the scripting api enough. You are right about the context that it will be difficult to run it in the UI thread but I thing this is possible. Where his scripts will be compiled into delegates that have the form as input and you just run the delegate in the UI Thread. Will be interesting to try and see how this can be done. – Filip Cordas Oct 12 '19 at 09:39
  • This is why I upvoted :-) Didn't know about the API and learned something today... – Martin Verjans Oct 12 '19 at 09:41
  • 1
    Right, I didn't know that. But I think it does not turn C# into a scripting language. Because you still need some compiled code and some plumbing to make this work. I think it does not answer the question of the OP but opens interesting solutions for more advanced scenarios – Daniel Schmid Oct 12 '19 at 09:59
  • As far as I can see the scripting API is not a lot more than an expression evaluation engine. Nice to know about but a long way short of a real scripting language, which would make all language features available. – TaW Oct 12 '19 at 10:32
  • 1
    @DanielSchmid After rereading the question you are probably right that he actually wants something else, Some sort of a parser to get the string. – Filip Cordas Oct 12 '19 at 10:33
  • @TaW No it's a full scripting engine vs uses it to provide you the c# interactive you can even load nuget packages with the newer versions check out https://github.com/filipw/dotnet-script for more real world examples on how to use it. – Filip Cordas Oct 12 '19 at 10:38