I'm not experienced with C#, but I got the basics down. Now I'm trying to download videos from YouTube with the Video Library (in the VS package manager: Install-Package VideoLibrary).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.IO;
using VideoLibrary;
namespace TubeDemo
{
public partial class MainWindow : Window
{
string link = "https://www.youtube.com/watch?v=8SbUC-UaAxE";
string link2 = "https://www.youtube.com/watch?v=BlRqTNkgEuo";
public MainWindow()
{
InitializeComponent();
}
void SaveVideoToDisk_Click(object sender, EventArgs e)
{
var youTube = YouTube.Default; // starting point for YouTube actions
var video = youTube.GetVideo(link2); // gets a Video object with info about the video
File.WriteAllBytes(#"C:\testfire\" + video.FullName, video.GetBytes());
}
}
}
Above functions SaveVideoToDisk_Click gets called from a .xaml button, which works fine. But not every video works OK. video.URI gets awfully big, over 800 characters. Some of the URLs turn out to cause the video.URI to throw an exception:
An unhandled exception of type 'System.ArgumentOutOfRangeException' occurred in mscorlib.dll
In the code provided, passing link as argument throws while passing link2 works just fine.
Can I fix this?
If not, how should I handle these exceptions? Just try, catch and report or is checking before a better idea?
Related
Trying to read the installation path of the game (InstallLocation) from one section in the registry (SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 4000), write it to another section (SOFTWARE\WpfApp) in key (NewInstallPath), and then find the exe file with the game in the installation path of the game, and run
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using Microsoft.Win32;
namespace wpfApp
{
/// \<summary\>
/// Interaction logic for MainWindow.xaml
/// \</summary\>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
private void Button_Click(object sender, RoutedEventArgs e)
{
using var localMachineRegistry = RegistryKey.OpenBaseKey(RegistryHive.LocalMachine, RegistryView.Registry64);
using var gmodAppRegistry = localMachineRegistry.OpenSubKey(#"SOFTWARE\Microsoft\Windows\CurrentVersion\Uninstall\Steam App 4000", false);
string readInstallPath = gmodAppRegistry.GetValue("InstallLocation").ToString();
RegistryKey InstallPath = Registry.LocalMachine.CreateSubKey(#"SOFTWARE\WpfApp", SetValue("NewInstallPath", readInstallPath)); //launcher partition path
}
}
}
As soon as I did not try to implement it, but even according to the documentation it comes out crooked. That value is recorded, but the game does not start, then nothing is recorded at all and does not start.
The way you're intended to run steam games is through steam itself.
Steam automatically updates the app etc.
There is a rungameid command line parameter which allows you to run a game ( somewhat ) directly.
"C:\Program Files (x86)\Steam\steam" steam://rungameid/281990
And... eventually as my login credentials install etc etc were stale...
Steam may be in C:\Program Files instead.
The nice-ish aspect of this is that you just need to check where steam is and know your game app id.
You can also run a game that isn't currently installed this way. There will of course be quite a delay as steam goes and gets everything for you.
I think some games are not so straight forward that they "just" have the one exe. Some have launchers ( ours does ).
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Forms;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Text.RegularExpressions;
using Microsoft.VisualBasic;
namespace HelloWorld
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
string filePathTEST = "";
public MainWindow()
{
InitializeComponent();
// Clearing text event handlers
textBox1.GotFocus += textBox1_GotFocus;
// Enter event handlers for textboxes
textBox1.KeyDown += new System.Windows.Input.KeyEventHandler(textBox1_KeyDown);
}
static void textBox1_KeyDown(object sender, System.Windows.Input.KeyEventArgs e)
{
if (e.KeyCode == Keys.Enter)
{
//enter key is down
}
}
The error I am getting when I try to run the above code is the following:
Cannot implicitly convert type
'System.Windows.Forms.KeyEventHandler' to
'System.Windows.Input.KeyEventHandler'
Then I tried changing the code to System.Windows.Input and then I get the following:
Error 1 'System.Windows.Input.KeyEventArgs' does not contain a
definition for 'KeyCode' and no extension method 'KeyCode' accepting a
first argument of type 'System.Windows.Input.KeyEventArgs' could be
found (are you missing a using directive or an assembly reference?)
The whole point of me doing this is that when I press enter on a textbox, I want to take the text in that textbox and populate a certain text file with it but I am not sure how to go about doing that.
The compiler thinks you mean to use 'System.Windows.Forms.KeyEventHandler' due to the namespace you've added: System.Windows.Forms.
Remove this line and your code should work:
using System.Windows.Forms;
Second, you should use Key instead of KeyCode since that is the WPF variant:
private void textBox1_KeyDown(object sender, KeyEventArgs e)
{
if (e.Key == Key.Return)
{
//
}
}
You're running foul of the rather confused state of Windows client development. System.Windows.Forms is part of the WinForms library, the original UI framework that shipped with .NET 1.0. The project you're working with is written in WPF (Windows Presentation Framework) the new lib that started shipping in .NET 3. There's a lot of overlap, but the two libraries are distinctive.
The class you want is System.Windows.Input.KeyEventArgs. If you go up onto MSDN for the documentation for this class, you'll see that it does NOT in fact have a KeyCode property. It does, however, have a Key property which should do what you want.
I'll leave actually finding and reading the MSDN documentation as an exercise for the reader. :-)
I'm writing an application in c# using wpf and i was wondering how do you create a data table in wpf? This is a really dumb question, i'm aware, but it doesn't appear like i'm using the correct references as the data table object never appears when i try to create it. My references are as follows:
using System;
using System.Collections.Generic;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using System.IO;
using System.Linq;
using System.ComponentModel;
using System.Data.Sql;
Here's a method that creates a DataTable...
private void CreateDataTable()
{
System.Data.DataTable dt = new DataTable("MyTable");
dt.Columns.Add("MyColumn", typeof (string));
dt.Rows.Add("row of data");
}
The relevant assembly (as identified by HighCore in the commentary) is System.Data. If it is not included in your references, you can add it via the 'add reference' context menu.
ive searched and searched the answer eludes me still. here is my code
using Microsoft.Phone.Controls;
using Microsoft.Phone.Shell;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Navigation;
namespace GTA_5_Guide
{
public partial class MainPage : PhoneApplicationPage
{
//Constructor
public MainPage()
{
//Loads the page onto the screen
InitializeComponent(); //This is were the error is thrown
}
}
}
the error thrown is a "A first chance exception of type 'System.Windows.Markup.XamlParseException' occurred in System.Windows.ni.dll"
everything was working fine till I decided not to use a web page I had found in my project anymore noe it throws this error
9 times out of 10 this is usually just the designer file not inheriting the same Type.
Make sure your MainPage root XAML control is in-face <phone:PhoneApplicationPage...
So I'm trying to build a program that accesses Etsy's API and so far all I am trying to do is make a call to it using OAuth and it throws this exception.
'The invocation of the constructor on type 'testEtsy.MainWindow' that matches the specified binding constraints threw an exception.' Line number '3' and line position '9'.
Here is my XAML
<Window x:Class="testEtsy.MainWindow"
xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
Title="MainWindow" Height="350" Width="525">
<Grid></Grid>
And here is my code in the MainWindow.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
namespace testEtsy
{
/// <summary>
/// Interaction logic for MainWindow.xaml
/// </summary>
public partial class MainWindow : Window
{
public MainWindow()
{
InitializeComponent();
}
string[] orderNumbers = System.IO.File.ReadAllLines(#"F:\ordernumbers.txt");
public static void getOAuthKey()
{
string ConsumerKey = "q6uqzk27z2yw4tl5s4qerdtp";
string ConsumerSecret = "tkjz2mu4x1";
OAuth.Manager m = new OAuth.Manager();
m["consumer_key"] = ConsumerKey;
m["consumer_secret"] = ConsumerSecret;
OAuth.OAuthResponse requestToken =
m.AcquireRequestToken("https://openapi.etsy.com/v2/oauth/request_token?scope=transactions_r", "POST");
}
}
}
Any help would be greatly appreciated.
The most likely of the exception is the following field initializer
string[] orderNumbers = System.IO.File.ReadAllLines(#"F:\ordernumbers.txt");
This code will run as a part of the MainWindow constructor. If an exception occurs while reading the file this will propragate out of the constructor and cause initialization to fail. The most likely cause is that this file doesn't exist or is inaccessible
The target of your project may not be the same as the target of a third party library. Just as a quick test, change your Platform target (under Project -> Properties -> Build) to x86. If that works, check that any external libs you have are 64 bit otherwise just build everything x86 instead of "Any CPU". For me the culprit was WebsocketSharp, for you looks like the OAuth library?