Jarvis in C# by Salim SR   2 years ago

17,775 views

127 Likes   7 Dislikes

Personal Assistant Demo C# Tutorial is coming soon ...
My Facebook Join Me At https://www.facebook.com/Marvel-Jarvis-1782290512099543/?qsefr=1
Here is the code for speech recognition :

//////////////here voice search ////////////////////////////
SpeechRecognitionEngine recognizer = new SpeechRecognitionEngine();
Grammar dictationGrammar = new DictationGrammar();
recognizer.LoadGrammar(dictationGrammar);
try
{
recognizer.SetInputToDefaultAudioDevice();
RecognitionResult result = recognizer.Recognize();
try
{
input.Text = result.Text;
input.Update();
}
catch (Exception)
{
computer.Speak("Could not recognize input from default aduio device. Is a microphone or sound card available");
}
finally
{
recognizer.UnloadAllGrammars();
}
}

//////////Search Button code ///////////////////

string urlAddress = input.Text;
webBrowser.Navigate("https://www.bing.com/search?q=/" + urlAddress);
/////////end here ///////////////////////////

thats it you done ...
///////////////////////Open file from base directory //////////////////////////////////////////////////////

String Path = System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().CodeBase);

Process process = new Process();

process.StartInfo = new ProcessStartInfo()
{
FileName = Path + "\\Documents\\somePDFfile.pdf"
};

process.Start();

//////////////////For Shutdown Restart Log Off Commands ////////////////////////
First, add this using namespace statements:

using System.Diagnostics;
using System.Runtime.InteropServices;

To shut down your computer, use this code:

Process.Start("shutdown","/s /t 0");

To restart your computer, use this code:

Process.Start("shutdown","/r /t 0"); // the argument /r is to restart the computer

To add this extern method to your class for (Log off)

[DllImport("user32")]
public static extern bool ExitWindowsEx(uint uFlags, uint dwReason);

Then, to log off, invoke the method:

ExitWindowsEx(0,0);

To lock your computer, add this extern method to your class:

[DllImport("user32")]
public static extern void LockWorkStation();

Then, to lock, invoke the method:

LockWorkStation();

To put your computer in Hibernate or Sleep, you need the same DllImport statement for them.

[DllImport("PowrProf.dll", CharSet = CharSet.Auto, ExactSpelling = true)]
public static extern bool SetSuspendState(bool hiberate, bool forceCritical, bool disableWakeEvent);

To bring your computer into Hibernate:

SetSuspendState(true, true, true);

And to bring it into sleep:

SetSuspendState(false, true, true);


///////////////////////////How to write text in notepad using textbox ////////////////////////////////////////////
using System;

using System.Collections.Generic;

using System.ComponentModel;

using System.Data;

using System.Diagnostics;

using System.Drawing;

using System.Linq;

using System.Runtime.InteropServices;

using System.Text;

using System.Threading.Tasks;

using System.Windows.Forms;


namespace MyForm

{

public partial class Form1 : Form

{

[DllImport("user32.dll", EntryPoint = "FindWindowEx")] public static extern IntPtr FindWindowEx(IntPtr hwndParent, IntPtr hwndChildAfter, string lpszClass, string lpszWindow);

[DllImport("User32.dll")] public static extern int SendMessage(IntPtr hWnd, int uMsg, int wParam, string lParam);

public Form1()

{

InitializeComponent();

}

private void Form1_Load(object sender, EventArgs e)

{

System.Diagnostics.Process.Start("notepad.exe");

}

private void button1_Click(object sender, EventArgs e)

{

Process[] notepads = Process.GetProcessesByName("notepad"); if (notepads.Length == 0) return; if (notepads[0] != null)

{

IntPtr child = FindWindowEx(notepads[0].MainWindowHandle, new IntPtr(0), "Edit", null);

SendMessage(child, 0x000C, 0, textBox1.Text);

}

}

}

}

Comments