Ramco Ramco

Ramco Ramco

  • 436
  • 3.4k
  • 546.4k

Video should open in same browser

Feb 26 2023 5:19 AM

Hi

   In below code Videos open in different 4 sessions. Is it possible that they should run in same 1 session,

On completion of first video next should run & so on

static async Task Main(string[] args)
{
    var datasource = @"MSSQLSERVER_2019";
    var database = "Videos";
    var username = "sa";
    var password = "sa";

    string connectionString = @"Data Source=" + datasource + ";Initial Catalog="
        + database + ";Persist Security Info=True;User ID=" + username + ";Password=" + password;
    List<string> youtubevideoslinks = GetYouTubeVideoLinksFromDatabase(connectionString);
    foreach (var item in youtubevideoslinks)
    {
        RunVideo(item);
    }
}
public static List<string> GetYouTubeVideoLinksFromDatabase(string connectionString)
{
    List<string> youtubevideoslinks = new List<string>();

    string query = "SELECT videoid FROM videos"; //Replace table name and column name here
    using (SqlConnection connection = new SqlConnection(connectionString))
    {
        SqlCommand command = new SqlCommand(query, connection);
        connection.Open();

        SqlDataReader reader = command.ExecuteReader();

        while (reader.Read())
        {
            string videoLink = reader["videoid"].ToString();
            youtubevideoslinks.Add(videoLink);
        }
        reader.Close();
    }

    return youtubevideoslinks;
}
public static void RunVideo(string videoId)
{
    string url = $"youtube.com/watch?v={videoId}";
    var process = new Process();
    var _process = new ProcessStartInfo(@"C:\Program Files (x86)\Microsoft\Edge\Application\msedge.exe", url);
    process.StartInfo = _process;
    process.Start();
    Thread.Sleep(2000);
    process.CloseMainWindow();
    process.Close();
}

Thanks


Answers (4)