Autor Beitrag
Spectus.gn
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 38

Win XP
C# (VS 2008 Pro)
BeitragVerfasst: Fr 15.09.06 17:18 
Hallo,
ich möchte ein programm schreiben mit Windows-forms-window.

Das problem ist folgendes.
In der Textbox soll eine Internetaddy stehen, die dann per Button.Click geladen wird und anschließen die Zeile 3 oder 4 ausgegeben wird. Ich habe leider keine Ahnung wie das funktionieren soll.

Vielleicht kann mir ja jemand helfen.

Benutze C# (VS2005Pro).

MfG
Spectus.gn

Danke schonmal für die Mühe

Moderiert von user profile iconChristian S.: Titel geändert.
Moderiert von user profile iconChristian S.: Topic aus IO, XML und Registry verschoben am Fr 15.09.2006 um 17:23
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Fr 15.09.06 17:24 
Hallo!

Ich kopier es einfach mal aus 'ner laufenden Anwendung von mir raus. Wenn Du Schwierigkeiten hast, es nach C# zu übersetzen, meld Dich nohcmal ;-)

ausblenden Chrome-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
method MainForm.GetHTMLSource(url : String) : List<String>;
begin
  var request := WebRequest.Create(url) as HttpWebRequest;               
  var response := request.GetResponse() as HttpWebResponse;
  using sr := new StreamReader(response.GetResponseStream, Encoding.UTF8) do
  begin
    result := new List<String>();    
    while not sr.EndOfStream do
      result.Add(sr.ReadLine);  
  end;
end;


Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Spectus.gn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 38

Win XP
C# (VS 2008 Pro)
BeitragVerfasst: Sa 16.09.06 11:48 
jab, hab ich... ;)

hab erst wieder angefangen und mein wissensstand ist gen mull.
danke schonmal
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 16.09.06 12:05 
Okay, hier das ganze in C#:

ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Text;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            List<String> lines = GetHTMLSource(@"http://www.delphi-forum.de");
            if (lines != null && lines.Count >= 4)
            {
                Console.WriteLine(lines[2]); //3. Zeile ausgeben
                Console.WriteLine(lines[3]); //4. Zeile ausgeben
            }
            Console.ReadLine();
        }

        static List<String> GetHTMLSource(string Url)
        {
            List<String> result = null;

            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();

            using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                result = new List<string>();
                while (!sr.EndOfStream)
                    result.Add(sr.ReadLine());
            }

            return result;
        }
    }
}


Ich finde, so unterschiedlich sind die Quelltexte gar nicht, nur dass man den hier jetzt halt per Copy & Paste übernehmen könnte :zwinker:

Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Spectus.gn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 38

Win XP
C# (VS 2008 Pro)
BeitragVerfasst: Sa 16.09.06 15:36 
Hi, danke für deine bemühungen.
Der Code ist gut, hat allerdings einen Haken.
wenn die Website jetzt 1000e Zeilen Quelltext hat und ich Zeile 2341 (z.B.) möchte wird der nie im leben fertig.
Hast du eine Idee, wie man den Code optimieren kann?
Danke schonmal

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

Optimieren hat geklappt. Rennt jetzt wie ne angestochene Maus.
Hast du eventuell auch noch ne Idee, wie ich von der Ausgelesenen Linie mir nur Zeichen 3 und 4 ausgeben lasse?
Mfg
Danke schonmal
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 16.09.06 16:06 
user profile iconSpectus.gn hat folgendes geschrieben:
Optimieren hat geklappt.
WIe hast Du optimiert?

user profile iconSpectus.gn hat folgendes geschrieben:
Hast du eventuell auch noch ne Idee, wie ich von der Ausgelesenen Linie mir nur Zeichen 3 und 4 ausgeben lasse?
Du kannst auf die Zeichen eines Strings zugreifen, wie auf die Elemente eines Arrays.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Spectus.gn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 38

Win XP
C# (VS 2008 Pro)
BeitragVerfasst: Sa 16.09.06 16:14 
Habe den Code wie folgt minimal abgeändert
ausblenden volle Höhe C#-Quelltext
1:
2:
3:
4:
5:
6:
7:
8:
9:
10:
11:
12:
13:
14:
15:
16:
17:
18:
19:
20:
21:
22:
23:
24:
25:
26:
27:
28:
29:
30:
31:
32:
33:
34:
35:
36:
37:
38:
39:
40:
using System;
using System.Net;
using System.IO;
using System.Collections.Generic;
using System.Text;


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            List<String> lines = GetHTMLSource(@"http://www.c-sharp-forum.de");
            Console.WriteLine(lines[16]); //17. Zeile ausgeben 
            Console.ReadLine();
        }


        static List<String> GetHTMLSource(string Url)
        {
            List<String> result = null;


            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(Url);
            HttpWebResponse response = (HttpWebResponse)request.GetResponse();


            using (StreamReader sr = new StreamReader(response.GetResponseStream(), Encoding.UTF8))
            {
                result = new List<string>();
                while (!sr.EndOfStream)
                    result.Add(sr.ReadLine());
            }


            return result;
        }
    }
}


Augrund der vielen zeilen habe ich die If-Abfrage weggelassen. Die Zeit ist auf ein viertel gefallen. Dein COde ist dennoch sehr gut.

kannst du mir schreiben, wie du das meinst mit dem auf die Strings zugreifen und so? :oops:
danke schonmal
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 16.09.06 16:38 
:shock: Die if-Abfrage dauert so lange? Da muss ich mich mal näher mit beschäftigen.

Zu den Zeichen im String:
ausblenden C#-Quelltext
1:
2:
string myString = "foobar";
Console.WriteLine(myString[2]); //3. Zeichen ausgeben


Grüße
Christian

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".
Spectus.gn Threadstarter
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 38

Win XP
C# (VS 2008 Pro)
BeitragVerfasst: Sa 16.09.06 16:43 
das Problem, dass das so lange gedauert hat bestand darin, dass die Website knapp 2,5k Zeilen hatte. Und ich ne Zeile um Nummer 1800 wollte. Darin bestand das problem. Dennoch war deine If-Abfrage kurz und knapp formuliert.
Danke.
Ich werde wahrscheinlich noch mehr fragen haben also thread bitte noch nicht schließen.
Danke nochmal.
Jetzt wird erstmal weitergetüftelt.
Christian S.
ontopic starontopic starontopic starontopic starontopic starontopic starhalf ontopic starofftopic star
Beiträge: 20451
Erhaltene Danke: 2264

Win 10
C# (VS 2019)
BeitragVerfasst: Sa 16.09.06 16:52 
Ist mir trotzdem unerklärlich. Die Prüfung auf "null" fällt definitiv nicht ins Gewicht und die Abfrage von "count" ist unabhängig von der Anzahl der Zeilen. Das sagt nicht nur die Doku ( ms-help://MS.VSCC.v80/MS.MSDN.v80/MS.NETFX30SDK4VS.1033/cpref8/html/P_System_Collections_Generic_List`1_Count.htm ), sondern kann man sich auch mittels des Reflectors ansehen.

Daher ist es mir ein Rätsel, wie die if-Abfrage soviel Zeit in Anspruch nehmen soll.


Threads schließen wir generell nicht, aber für neue Fragen sollte ein neuer Thread erstellt werden, damit es übersichtlich bleibt.

_________________
Zwei Worte werden Dir im Leben viele Türen öffnen - "ziehen" und "drücken".