Having trouble getting CPU temps using Open Hardware Monitor C# - c#

As the title describes I am having trouble getting CPU temps using the openhardwaremonitor.dll reference. I think I am running the program as an admin, I select run as admin when starting visual studio because I do not have the option to add an application.manifest file. This is the code I am using;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using OpenHardwareMonitor.Hardware;
namespace Get_CPU_Temp5
{
class Program
{
public class UpdateVisitor : IVisitor
{
public void VisitComputer(IComputer computer)
{
computer.Traverse(this);
}
public void VisitHardware(IHardware hardware)
{
hardware.Update();
foreach (IHardware subHardware in hardware.SubHardware) subHardware.Accept(this);
}
public void VisitSensor(ISensor sensor) { }
public void VisitParameter(IParameter parameter) { }
}
static void GetSystemInfo()
{
UpdateVisitor updateVisitor = new UpdateVisitor();
Computer computer = new Computer();
computer.Open();
computer.CPUEnabled = true;
computer.Accept(updateVisitor);
for (int i = 0; i < computer.Hardware.Length; i++)
{
if (computer.Hardware[i].HardwareType == HardwareType.CPU)
{
for (int j = 0; j < computer.Hardware[i].Sensors.Length; j++)
{
if (computer.Hardware[i].Sensors[j].SensorType == SensorType.Temperature)
Console.WriteLine(computer.Hardware[i].Sensors[j].Name + ":" + computer.Hardware[i].Sensors[j].Value.ToString() + "\r");
}
}
}
computer.Close();
}
static void Main(string[] args)
{
while (true)
{
GetSystemInfo();
}
}
}
}
Error;
"System.IO.FileNotFoundException: 'Could not load file or assembly 'System.Management, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'. The system cannot find the file specified.'"
This error is at computer.Open();
I am using visual studio 2019.
Any help will be greatly appreciated!

I just ran into this issue with trying to use openhardwaremonitor.dll with .Net core. The fix I found was to load system.management from Nuget. Apparently the system.management assemblies are not included in .net core like they are with .net Framework.

Related

Controll Motor Driver HAT Waveshare with C#

I am very new in this. So if anyone can help will be great. I am trying to control Motor Driver HAT Waveshare with C# via i2c from my Raspberry pi 4b. I Install Iot.Device.MotorHat NuGet package and try this example but the result is Unexpected value of duty cycle (9766, 10280).
This is the i2c addres
This is my class
using Iot.Device.MotorHat;
using System.Collections.Generic;
using System.Device.I2c;
namespace projectV2
{
public class DCMotorController : BaseClass
{
public void StartMotor()
{
using (var motorHat = new MotorHat(200, 0x40))
{
var motor = motorHat.CreateDCMotor(1);
motor.Speed = 1;
}
}
public void StopMotor()
{
using (var motorHat = new MotorHat(200, 0x40))
{
var motor = motorHat.CreateDCMotor(1);
motor.Speed = 0;
}
}
}
}

How to play a simple audio file with Xamarin iOS/Android

I'm trying to learn and play a bit with Xamarin :)
I wanted to play a simple sound at the end of a certain time and in part I succeeded, this works on the android and ios emulators but when I try to build the app on my iPhone this crush at the moment of sound reproduction!
the code I wrote I copied from here!
so my code is this:
iAudio.cs :
using System;
namespace StopWatch
{
public interface IAudio
{
void PlayAudioFile(string fileName);
}
}
AudioService.cs in Android :
using System;
using Xamarin.Forms;
using StopWatch.Droid;
using Android.Media;
using Android.Content.Res;
[assembly: Dependency(typeof(AudioService))]
namespace StopWatch.Droid
{
public class AudioService : IAudio
{
public AudioService()
{ }
public void PlayAudioFile(string fileName)
{
var player = new MediaPlayer();
var fd = global::Android.App.Application.Context.Assets.OpenFd(fileName);
player.Prepared += (s, e) =>
{
player.Start();
};
player.SetDataSource(fd.FileDescriptor, fd.StartOffset, fd.Length);
player.Prepare();
}
}
}
AudioService.cs in iOS:
using System; using Xamarin.Forms; using StopWatch; using StopWatch.iOS; using System.IO; using Foundation; using AVFoundation; [assembly: Dependency(typeof(AudioService))] namespace StopWatch.iOS {
public class AudioService : IAudio
{
public AudioService()
{ }
public void PlayAudioFile(string fileName)
{
string sFilePath = NSBundle.MainBundle.PathForResource(Path.GetFileNameWithoutExtension(fileName), Path.GetExtension(fileName));
NSUrl url = NSUrl.FromString(sFilePath);
var _player = AVAudioPlayer.FromUrl(url);
_player.FinishedPlaying += (object sender, AVStatusEventArgs e) =>
{
_player = null;
};
_player.Play();
}
}
}
and this is mi MainPage.xaml.cs :
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using Xamarin.Forms;
namespace StopWatch
{
// Learn more about making custom code visible in the Xamarin.Forms previewer
// by visiting https://aka.ms/xamarinforms-previewer
[DesignTimeVisible(false)]
public partial class MainPage : ContentPage
{
Stopwatch stopwatch;
public MainPage()
{
InitializeComponent();
stopwatch = new Stopwatch();
lblStopWatch.Text = "00:00:00";
lblStopWatchAllert.Text = "";
}
private void btnStartClicked(object sender, EventArgs e)
{
stopwatch.Start();
Device.StartTimer(TimeSpan.FromMilliseconds(10), () =>
{
lblStopWatch.Text = stopwatch.Elapsed.ToString().Substring(0, 8);
controll(lblStopWatch.Text);
return true;
});
}
private void btnStopClicked(object sender, EventArgs e)
{
stopwatch.Stop();
}
private void btnResetClicked(object sender, EventArgs e)
{
stopwatch.Reset();
}
private void controll(String time)
{
if (string.Compare(time, "00:00:03:0000") > 0)
{
stopwatch.Reset();
lblStopWatchAllert.Text = "time is over!";
DependencyService.Get<IAudio>().PlayAudioFile("Alert.mp3");
}
}
}
}
the code crashes me at this point of the iOS AudioService.cs file:
I think the problem lies in the info.plist (although I am most likely wrong) but I don't know how to solve it :(
can someone help me? thank you
I have checked this code , it works in my site . Invoking the play method as follow :
DependencyService.Get<IAudio>().PlayAudioFile("Alan_Walker.mp3");
The other codes are the same with yours in iOS and Android , they all play the sound successfully.
Here you need to notice that ,the local sound file should be added to each platform .And each platform has its specical folder to put the auido file .
In Android , you need to put it in Assets folder as follow :
And in iOS , you need to put it in Resources folder as follow :
==========================Update===================================
You should first add file to this folder as follow :
Then add Existing file to this folder :
Then when installing app , this filw will be added to mobile . No matter what is a simulator or a physical device.
Note :
Not copying file from other path to Xamarin.iOS project , this can't make sure the file be added to the project .

Referencing assemblies in Windows Phone 8.1 in Visual Studio 2015

what do I have to reference in Windows Phone 8.1 project under Visual Studio 2015 for Linq, ObservableCollection etc.?
I'm getting this error, but only in the Watch window!:
groups error CS0012: The type 'IGrouping<,>' is defined in an assembly that is not referenced. You must add a reference to assembly 'System.Linq, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a'.
But this assembly is not builded for Windows Universal Apps. So what assembly package should I reference? Same is for the ObservableCollection<>
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Windows.UI;
namespace GoodCode.GTDApp.Models
{
public class PageModel
{
public ObservableCollection<ItemModel> Items { get; set; }
public PageModel()
{
this.Items = new ObservableCollection<ItemModel>();
this.Items.CollectionChanged += Items_CollectionChanged;
}
private void Items_CollectionChanged(object sender, System.Collections.Specialized.NotifyCollectionChangedEventArgs e)
{
var groups = this.Items.GroupBy(s => s.Date);
foreach (var group in groups.OrderByDescending(s => s.First().Date))
{
foreach (var item in group)
{
item.Color = Colors.Red;
}
}
}
}
}

MEF based plugin system can't instance my plugins

I've implemented a very small plugin system based on C# with MEF. The problem is, none of my plugins are instanced. In the Aggregate-Catalog I can see my plugin listed. But, after I'll compose these parts, there isn't my plugin in the plugin list, what I'm doing wrong?
Here's a snippet of my code:
Plugin-Loader:
[ImportMany(typeof(IFetchService))]
private IFetchService[] _pluginList;
private AggregateCatalog _pluginCatalog;
private const string pluginPathKey = "PluginPath";
...
public PluginManager(ApplicationContext context)
{
var dirCatalog = new DirectoryCatalog(ConfigurationManager.AppSettings[pluginPathKey]);
//Here's my plugin listed...
_pluginCatalog = new AggregateCatalog(dirCatalog);
var compositionContainer = new CompositionContainer(_pluginCatalog);
compositionContainer.ComposeParts(this);
}
...
And here, the plugin itself:
[Export(typeof(IFetchService))]
public class MySamplePlugin : IFetchService
{
public MySamplePlugin()
{
Console.WriteLine("Plugin entered");
}
...
}
Tested working sample.
Compile class library with code inside PluginNameSpace namespace and place it to the 'Test' folder which will be inside console app exe folder.
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.IO;
using System.Reflection;
using ConsoleApplication;
namespace ConsoleApplication
{
public interface IFetchService
{
void Write();
}
class PluginManager
{
[ImportMany(typeof(IFetchService))]
public IFetchService[] PluginList;
public PluginManager()
{
var dirCatalog = new DirectoryCatalog(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + "\\Test");
var pluginCatalog = new AggregateCatalog(dirCatalog);
var compositionContainer = new CompositionContainer(pluginCatalog);
compositionContainer.ComposeParts(this);
}
}
class Program
{
static void Main(string[] args)
{
var pluginManager = new PluginManager();
foreach (var fetchService in pluginManager.PluginList)
{
fetchService.Write();
}
Console.ReadKey();
}
}
}
// Separate class library
namespace PluginNameSpace
{
[Export(typeof(IFetchService))]
public class MySamplePlugin : IFetchService
{
public void Write()
{
Console.WriteLine("Plugin entered");
}
}
}

C# speech recogntion engine recognizes everything wrong

I have been trying to make a personal assistant in my free time, and so far i have made him speak, but now i am trying to speak to him. Whenever i do however, he fails massively. When i say "Hello my name is Alexander" he recognizes "in the name is unresolved bush" or something else that is just not correct. am i doing something wrong or is the built in C# recognition engine just broken?
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Speech.Recognition;
using System.Speech.Synthesis;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace TTS_Test
{
class Jarvis
{
private static SpeechSynthesizer synthezier;
private static String name;
public Jarvis()
{
synthezier = new SpeechSynthesizer();
synthezier.SelectVoiceByHints(VoiceGender.Male, VoiceAge.Senior);
synthezier.Volume = 100;
synthezier.Rate = 0;
}
private bool isFirstTime()
{
if (File.Exists("config"))
{
return false;
}else{
return true;
}
}
private void firstTimeSetup()
{
say("Hello, My name is Jarvis. It seems that this is your first time here. Please take some time to configure the application.");
Config config = new Config();
config.ShowDialog();
say("Thank you! I should be up and running now.");
}
public void initiate()
{
if (isFirstTime())
{
firstTimeSetup();
}
setupUserData();
say("Hello " + name+". How may i help you today?");
recognize();
}
public void setupUserData()
{
StreamReader reader = new StreamReader("config");
name = reader.ReadLine();
reader.Close();
}
public void say(string output)
{
synthezier.Speak(output);
}
public void recognize()
{
SpeechRecognitionEngine sr = new SpeechRecognitionEngine(new System.Globalization.CultureInfo("en-GB"));
sr.LoadGrammar(new DictationGrammar());
sr.InitialSilenceTimeout = TimeSpan.FromSeconds(5);
sr.SetInputToDefaultAudioDevice();
RecognitionResult result = sr.Recognize();
MessageBox.Show(result.Text);
}
}
}
You should train your computer to better understand you by going to the Control Panel\All Control Panel Items\Speech Recognition

Categories

Resources