milosev.com
  • Home
    • List all categories
    • Sitemap
  • Downloads
    • WebSphere
    • Hitachi902
    • Hospital
    • Kryptonite
    • OCR
    • APK
  • About me
    • Gallery
      • Italy2022
    • Curriculum vitae
      • Resume
      • Lebenslauf
    • Social networks
      • Facebook
      • Twitter
      • LinkedIn
      • Xing
      • GitHub
      • Google Maps
      • Sports tracker
    • Adventures planning
  1. You are here:  
  2. Home
  3. C#

IsNullOrEmpty

Details
Written by: Stanko Milosev
Category: Beginning
Published: 11 February 2013
Last Updated: 11 February 2013
Hits: 4173

If varible is string, then to check if it is null use:

if (containerid.IsNullOrEmpty())
{  containerid = "-1"; }

Or String.IsNullOrEmpty()

Because null is not empty...

Unsafe code may only appear if compiling with /unsafe

Details
Written by: Stanko Milosev
Category: Beginning
Published: 16 August 2008
Last Updated: 05 November 2009
Hits: 6663

In VS.NET go to the project property page and in configuration properties>build set Allow Unsafe Code Blocks to True.


Taken from here.

Example of WebClient

Details
Written by: Stanko Milosev
Category: Code snippets
Published: 21 December 2019
Last Updated: 10 February 2022
Hits: 1511
Just a small example of WebClient:
string milosevCom = "http://www.milosev.com";
string doc = "";
using (System.Net.WebClient client = new System.Net.WebClient())
{
	doc = client.DownloadString(milosevCom);
}

Console.WriteLine(doc);
Console.ReadKey();
WebClient is from .NET 6 obsolete.

Selenium in Mono under Ubuntu

Details
Written by: Stanko Milosev
Category: Code snippets
Published: 06 June 2015
Last Updated: 06 April 2022
Hits: 7399

Here I explained how to install MonoDevelop. As I already explained here, first download Selenium Client & WebDriver Language Bindings, in my case I downloaded this one. You will also need ChromeDriver (this time for linux), in my case I downloaded this one. 

If you try to use chromedriver for windows you will receive errors like:

OpenQA.Selenium.WebDriverException: Unexpected error. System.Net.WebException: Error: ConnectFailure (Connection refused) ---> System.Net.Sockets.SocketException: Connection refused
at System.Net.Sockets.Socket.Connect (System.Net.EndPoint remoteEP) [0x000f7] in <filename unknown>:0
at System.Net.WebConnection.Connect (System.Net.HttpWebRequest request) [0x0019b] in <filename unknown>:0
--- End of inner exception stack trace ---
at System.Net.HttpWebRequest.EndGetRequestStream (IAsyncResult asyncResult) [0x00043] in <filename unknown>:0
at System.Net.HttpWebRequest.GetRequestStream () [0x00057] in <filename unknown>:0
at OpenQA.Selenium.Remote.HttpCommandExecutor.Execute (OpenQA.Selenium.Remote.Command commandToExecute)

and:

run-detectors: unable to find an interpreter for /home/stanko/Downloads/SeleniumTest/SeleniumTest/bin/Debug/chromedriver.exe

Also if you in terminal window write something like:

mono chromedriver.exe

Then you will receive error like:

Cannot open assembly 'chromedriver.exe': File does not contain a valid CIL image

That is why we need to use Linux version of chromedriver.

Download and install Chrome.

Open MonoDevelop, start new solution:

choose console project:

After creating project right click on references:

 

switch to .Net Assembly tab, and add selenium dll's which we downloaded previously. 

Now open Program.cs, in using section write:

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

In main method write something like:

ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"/home/myUserName/projects/SeleniumTest/lib/chromedriver_linux64", "chromedriver");
IWebDriver driver = new ChromeDriver(service);
driver.Navigate ().GoToUrl ("http://www.milosev.com");

Notice line:

ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"/home/myUserName/projects/SeleniumTest/lib/chromedriver_linux64", "chromedriver");

This line we need to tell selenium that we will use Linux chromedriver, otherwise Selenium will always try to load chromedriver.exe (Windows one).

Whole application should look like this:

using System;

using OpenQA.Selenium;
using OpenQA.Selenium.Chrome;

namespace SeleniumTest
{
	class MainClass
	{
		public static void Main (string[] args)
		{
			ChromeDriverService service = ChromeDriverService.CreateDefaultService(@"/home/myUserName/projects/SeleniumTest/lib/chromedriver_linux64", "chromedriver");
			IWebDriver driver = new ChromeDriver(service);
			driver.Navigate ().GoToUrl ("http://www.milosev.com");
		}
	}
}

Of course path "/home/myUserName/projects/SeleniumTest/lib/chromedriver_linux64" change to path where you downloaded your chromedriver.

Example download from here, and don't forget to use http in your URL's otherwise Selenium will not work.

  1. Selenium in .NET
  2. Execute sql scripts from console
  3. OCR with MODI
  4. Beginner's All-Purpose Symbolic Instruction Code

Subcategories

WPF

Beginning

Code snippets

NUnit

LINQ

Windows Forms

Page 28 of 33

  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32