How to include/run external C# script into my project

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
11
Баллы
18
I have a feeling this is really easy but can't get it working. I would like to be able to include and run an external c# script into my template.

I don't want to use the C# code module as I want to be able to upgrade my external c# without manually changing the zenno templates.
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 180
Баллы
113
You can compile your C# script in .exe and then run it from template using Own code action.
Other way would be to use Shared code option in zenno and then run your script from file.

Its not clear what you want to do.
 

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
11
Баллы
18
You can compile your C# script in .exe and then run it from template using Own code action.
You mean using Run program action? If yes, no, that's not what I want to do. This is what I try to avoid and this is the reason I switched from python to C#, so I can integrate as much code into my zenno templates.

I want that instead of writing my code into the C# code module (Own C# box) to be able to run that same code from an external file that I can modify externally without modifying the template. The reason behind this is because I have multiple templates running the same C# code and if I update the code I would have to manually edit every template so I can change the code. Using and importing an external file would be much easier.
 
Последнее редактирование:

PHaRTnONu

Client
Регистрация
01.10.2016
Сообщения
340
Благодарностей
48
Баллы
28
I want that instead of writing my code into the C# code module (Own C# box) to be able to run that same code from an external file that I can modify externally without modifying the template. The reason behind this is because I have multiple templates running the same C# code and if I update the code I would have to manually edit every template so I can change the code. Using and importing an external file would be much easier.
???? just compile it in a dll and run the action that way you only have to update whats in the external assembly?
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 180
Баллы
113
You mean using Run program action? If yes, no, that's not what I want to do. This is what I try to avoid and this is the reason I switched from python to C#, so I can integrate as much code into my zenno templates.

I want that instead of writing my code into the C# code module (Own C# box) to be able to run that same code from an external file that I can modify externally without modifying the template. The reason behind this is because I have multiple templates running the same C# code and if I update the code I would have to manually edit every template so I can change the code. Using and importing an external file would be much easier.
Yes then check Own code and tab shared code, there you can pick that this code is read from file. So if you edit code it will be edited and all templates will use that file.
You can also use Visual Studio to develop code and then add that to Zenno libraries.
 

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
11
Баллы
18
I have just started learning C# and knowing other languages helped me to quickly understand what's going on so creating C# scripts it's not an issue for me right now but rather integrating that code with Zenno is the real pain. I have also searched for tutorials on how to do basic tasks but couldn't find any or what I've found, are in Russian. Even if I do my best to understand from what I see in the videos and by translating, the language barrier is too much for me at this point.

???? just compile it in a dll and run the action that way you only have to update whats in the external assembly?
How can I run the compiled c# dll'sl in zenno?

Yes then check Own code and tab shared code, there you can pick that this code is read from file. So if you edit code it will be edited and all templates will use that file.
You can also use Visual Studio to develop code and then add that to Zenno libraries.
I have loaded my .cs code into the shared code box but I need to know how can I run/use this code?
I'm using Visual Studio 2017 to develop my code but don't really know how to add it to the Zenno libraries. Moreover, my code works well in VS but when I try to run it in Own C# box for example I get alot of errors.

Let's take this line of code for example:

Код:
 MatchCollection foundItems = Regex.Matches(text, $@"\b{k}\b", RegexOptions.IgnoreCase);
This line is perfectly valid in VS but in Zenno it generates an error: CS056 "Unexpected character '$''.

Is it enough to get my variables from the zenno template and then I can use the exact same code from Visual Studio or is there anything else I need to do so I can make my C# scripts work in Zenno?

I managed to run code like this, for example, using Own C# box:

Код:
string text = project.Variables["para"].Value;
string link = project.Variables["link"].Value;
var keywords = project.Lists["keywords"];
bool active = true;

for (int k=0; k < keywords.Count; k++)
{

if (text.Contains(keywords[k]))
{
text = text.Replace(keywords[k], "<a href=\"" + link + "\">" + keywords[k] + "</a>");
active = false;
break;
}
}

if (active == true)
{
text = text + "<br><a href=\"" + link + "\">" + link + "</a>";
Console.WriteLine(text);
}


return text;
But I can't get more elaborate scripts to work.

Bottomline - what's the best place to start learning how to integrate c# into zenno? Like I said, I just started learning so I need the basic stuff.
 

PHaRTnONu

Client
Регистрация
01.10.2016
Сообщения
340
Благодарностей
48
Баллы
28

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 180
Баллы
113
Let's take this line of code for example:

Код:
 MatchCollection foundItems = Regex.Matches(text, $@"\b{k}\b", RegexOptions.IgnoreCase);
This line is perfectly valid in VS but in Zenno it generates an error: CS056 "Unexpected character '$''.
As this example error totally shows what is problem, I'm not sure why this code line works in Visual Studio for you because if does not for me. Its wrong syntax,

 

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
11
Баллы
18
Have 1 command exicuti the whole thing
I still don't get how can I execute the whole thing. Can you provide a simple example?

As this example error totally shows what is problem, I'm not sure why this code line works in Visual Studio for you because if does not for me. Its wrong syntax,
Код:
foreach (var k in keywords)
            {

                MatchCollection foundItems = Regex.Matches(text, $@"\b{k}\b", RegexOptions.IgnoreCase);
                var countItems = foundItems.Count;
This is what's preceding that code, maybe it makes sense to you but the code definitely works in VS 2017
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 180
Баллы
113
I still don't get how can I execute the whole thing. Can you provide a simple example?


Код:
foreach (var k in keywords)
            {

                MatchCollection foundItems = Regex.Matches(text, $@"\b{k}\b", RegexOptions.IgnoreCase);
                var countItems = foundItems.Count;
This is what's preceding that code, maybe it makes sense to you but the code definitely works in VS 2017
In this code there are two syntax errors. First is what I told you already. This is not string $@"\b{k}\b" dollar sign is not needed there. And second one are your foreach loop is not closed. So it can not work because code is wrong.

I suggest you to learn more.

Good luck.
 

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
11
Баллы
18
In this code there are two syntax errors. First is what I told you already. This is not string $@"\b{k}\b" dollar sign is not needed there. And second one are your foreach loop is not closed.
By "not needed" you mean it's not mandatory or is it wrong to be there? I mean, the code could run without it or will only run if it's not there?

For the second error you mentioned, I know the foreachloop it's not closed as it has 90 more lines and like I said, this code is preceding the line I was posted initially in my 1st post.
I invite you to run my whole code below and see if it works or not.

Код:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Threading.Tasks;

namespace textProcessorMap
{

    public class Program
    {

        public static string tagAdder(string text, string startTag, string endTag, int startIndex, int lengthWord)
        {

            text = text.Insert(startIndex, startTag);

            int tagStartLength = startTag.Length;
            int endIndex = startIndex + lengthWord + tagStartLength;

            int tagEndLength = endTag.Length;
            text = text.Insert(endIndex, endTag);
            return text;
            //int endReserved = startIndex + tagStartLength + lengthWord + tagEndLength;

        }


        static void Main(string[] args)
        {

            //List<int> reservedStart = new List<int>(new int[] { 0 });
            //List<int> reservedEnd = new List<int>(new int[] { 0 });

            List<int> reservedStart = new List<int>();
            List<int> reservedEnd = new List<int>();

            string text = "Zennoposter it's a great piece of software but without proper documentation little tasks take alot of time and instead of coding some amazing bots I struggle understanding the meaning of the english words. Like every other great software it will fade if it's not properly maintained by its community as history repeats itself.";
            List<string> keywords = new List<string>(new string[] { "bbb", "zennoposter", "bots", "software" });
            string link = "http://zennolab.com/en/";
            string link2 = "http://zennolab.com/ru/";
            string linkStart = "<a href\"=" + link + "\">";
            string linkEnd = "</a>";
            string linkStart2 = "<a href\"=" + link2 + "\">";

            int tagCounter = 0;
            bool ok = true;

            foreach (var k in keywords)
            {

                MatchCollection foundItems = Regex.Matches(text, $@"\b{k}\b", RegexOptions.IgnoreCase);
                var countItems = foundItems.Count;
                if (countItems > 0)
                {

                    if (tagCounter == 0) //main link adding
                    {

                        foreach (Match cucu in foundItems)
                        {
                            ok = true;
                            int startR = cucu.Index;
                            //int endR = foundItems[rndKey].Index + linkEnd.Length;

                            for (int r = 0; r < reservedStart.Count; r++)
                            {
                                if (startR >= reservedStart[r] && startR <= reservedEnd[r])
                                {
                                    ok = false;
                                    break;
                                }

                            }

                            if (ok == true)
                            {
                                text = tagAdder(text, linkStart, linkEnd, cucu.Index, cucu.Length);
                                reservedStart.Add(cucu.Index);
                                int endR = cucu.Index + linkStart.Length + cucu.Length + linkEnd.Length;
                                reservedEnd.Add(endR);
                                tagCounter++;
                                ok = true;
                                break;
                            }
                            //tagAdder(text, linkStart, linkEnd, foundItems[rndKey].Index, foundItems[rndKey].Length);

                            //reservedStart.Add(foundItems[rndKey].Index);
                            //reservedEnd.Add(endR);
                        }
                    }
                    else if (tagCounter == 1)
                    {

                        foreach (Match cucu in foundItems)
                        {
                            ok = true;
                            int startR = cucu.Index;
                            //int endR = foundItems[rndKey].Index + linkEnd.Length;

                            for (int r = 0; r < reservedStart.Count; r++)
                            {
                                if (startR >= reservedStart[r] && startR <= reservedEnd[r])
                                {
                                    ok = false;
                                    break;

                                }

                            }

                            if (ok == true)
                            {
                                text = tagAdder(text, linkStart2, linkEnd, cucu.Index, cucu.Length);
                                reservedStart.Add(cucu.Index);
                                int endR = cucu.Index + linkStart.Length + cucu.Length + linkEnd.Length;
                                reservedEnd.Add(endR);
                                tagCounter++;
                                break;
                            }





                        }

                    }

                }



            }

            Console.WriteLine(text);

        }
    }
}


The code as it is will add a href tags to the keyword found inside the text returning the final text with the html added. Now I want to run this code inside my zenno template, at a certain location while aslo making it possible to modify the c# from outside the template.


Running any element in my template will show this error:


So I want to take the string text, the keywords list and strings link and link2 from my template - run the c# and return the final text. How can I achieve that?
 
Последнее редактирование:

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 180
Баллы
113
In short You can not run code in C# action and edit it externally.

There were suggestions that you have to make either .dll file as library and then add it to zenno. Or use shared code and then from file. But you can not just copy any code and run it. You have to lern how to develop in C# at least at very basic level, to avoid such mistakes.

MatchCollection foundItems = Regex.Matches(text, $@"\b{k}\b", RegexOptions.IgnoreCase);

Your error says Unexpected character $ This is exactly right because right line will be:

MatchCollection foundItems = Regex.Matches(text, @"\b{k}\b", RegexOptions.IgnoreCase);

Exactly same but without $ -

Cheers
 

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
11
Баллы
18
It is true I have just started learning C# and right now I'm doing more like a parkour programming - getting from A to B in the shortest time as possible. On the other hand having Visual Studio debug it's pretty easy to see what should I do - the problem is in zenno where I don't really know what I'm doing wrong.

Anyways, I will try to use shared code and/or GAC references to include my script into the template. Is there anything I can read to help me with that? DO I need to make a class and call it from the c# code box ?
 

lokiys

Moderator
Регистрация
01.02.2012
Сообщения
4 770
Благодарностей
1 180
Баллы
113
It is true I have just started learning C# and right now I'm doing more like a parkour programming - getting from A to B in the shortest time as possible. On the other hand having Visual Studio debug it's pretty easy to see what should I do - the problem is in zenno where I don't really know what I'm doing wrong.

Anyways, I will try to use shared code and/or GAC references to include my script into the template. Is there anything I can read to help me with that? DO I need to make a class and call it from the c# code box ?
All I can suggest is forum search, googole search and youtube search. This is only places I have used to learn C# and use it in zenno.

Cheers
 
  • Спасибо
Реакции: zenfreak

zenfreak

Client
Регистрация
21.08.2013
Сообщения
249
Благодарностей
11
Баллы
18

Кто просматривает тему: (Всего: 1, Пользователи: 0, Гости: 1)