Skip to content

Instantly share code, notes, and snippets.

@emoacht
emoacht / BatteryStatus.cs
Last active August 29, 2015 14:13
Check battery status.
using System.ComponentModel;
using System.Runtime.CompilerServices;
using System.Windows.Forms;
public class BatteryStatus : INotifyPropertyChanged
{
public BatteryChargeStatus BatteryChargeStatus => SystemInformation.PowerStatus.BatteryChargeStatus; // Flag
public int BatteryFullLifetime => SystemInformation.PowerStatus.BatteryFullLifetime;
@emoacht
emoacht / StreamCopy.cs
Created January 28, 2015 13:12
Copy a file with Stream.
using System;
using System.Collections.Concurrent;
using System.Diagnostics;
using System.IO;
using System.Threading;
using System.Threading.Tasks;
public class StreamCopy
{
private const int bufferSize = 1024 * 1024; // 1MiB
@emoacht
emoacht / BytesExtension.cs
Last active August 29, 2015 14:15
Extension method for Byte array
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
public static class BytesExtension
{
public static byte[] SequenceReplace(this byte[] source, byte[] oldValue, byte[] newValue, int maxCount = -1)
{
var sourceIndices = SequenceIndicesOf(source, oldValue, maxCount).ToArray();
@emoacht
emoacht / ExifDate.cs
Last active August 29, 2015 14:15
Change Exit date data in JPEG files.
// Add reference to WindowsBase and System.Xaml.
using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Media.Imaging;
public class ExifDate
@emoacht
emoacht / DeviceNotificationListener.cs
Created February 15, 2015 09:02
Listen add/remove events of devices including USB devices in WPF
using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Windows;
using System.Windows.Interop;
@emoacht
emoacht / OperarionNew.cs
Last active August 29, 2015 14:16
ReactivePropertyでObservableCollection中の要素のPropertyを購読したい。
using System.Collections.ObjectModel;
using System.Linq;
using System.Reactive.Linq;
using System.Threading.Tasks;
using Reactive.Bindings;
using Reactive.Bindings.Extensions;
public class Operation : BindableBase
{
public ObservableCollection<ProfileItem> Profiles
@emoacht
emoacht / App.xaml.cs
Created March 8, 2015 01:06
Check an app's DPI awareness.
public partial class App : Application
{
protected override void OnStartup(StartupEventArgs e)
{
base.OnStartup(e);
MessageBox.Show(
String.Format("IsProcessDPIAware: {0}", IsProcessDPIAware()) + Environment.NewLine +
String.Format("GetDpiAwareness: {0}", GetDpiAwareness()));
}
@emoacht
emoacht / DisplayManager.cs
Created March 10, 2015 05:03
Manage display devices.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Runtime.InteropServices;
using System.Windows;
using System.Windows.Interop;
using System.Windows.Media;
public static class DisplayManager
{
@emoacht
emoacht / Base.cs
Last active October 21, 2020 01:33
ReactivePropertyを使ってコレクション中の要素のプロパティ変更をFunc<T, bool>で指定した方法でまとめてIObservable<bool>にする。
public ObservableCollection<Person> People = new ObservableCollection<Person>();
public ReactiveProperty<bool> IsAnyRemoved;
// 元のメソッド
private void SubscribePeople0()
{
Func<Person, bool> predicate = x => x.IsRemoved;
var peopleRemoved = new ObservableCollection<Person>();
@emoacht
emoacht / FilteredReadOnlyObservableCollection.cs
Created March 16, 2015 20:11
FilteredReadOnlyObservableCollectionからReactiveProperty<bool>で購読する
// http://okazuki.hatenablog.com/entry/2015/03/17/025940
var filtered = source.ToFilteredReadOnlyObservableCollection(x => x.IsRemoved);
ReactiveProperty<bool> IsAnyRemoved = filtered
.CollectionChangedAsObservable()
.Select(_ => 0 < filtered.Count)
.ToReactiveProperty();