public static X509Certificate2 GetCertStorePass(string pin, X509Certificate2 certificate2) { var provider = (Gost3410CryptoServiceProvider)certificate2.PrivateKey; var secure = new System.Security.SecureString(); foreach (char charPass in pin) secure.AppendChar(charPass); provider.SetContainerPassword(secure); return provider.ContainerCertificate; }
c# Get a certificate from a container and set a pin / password programmatically
C# Получаем короткое имя из Subject сертификата
public static string GetSubjectCN(X509Certificate2 cert) { // вырезаем имя из строки int indexStart = cert.Subject.IndexOf("CN"); if (indexStart >= 0) { int indexEnd = cert.Subject.IndexOf(",", indexStart); indexStart += 3; if (indexStart < indexEnd) { return cert.Subject.Substring(indexStart, indexEnd - indexStart); } } return null; }
C# How to get all certificates from "My" Store
public static List<string> GetCertNamesFromStore(string search) { X509Store store = new X509Store("My"); try { store.Open(OpenFlags.ReadOnly); X509Certificate2Collection certCollection = store.Certificates; X509Certificate2Collection currentCerts = certCollection.Find(X509FindType.FindByTimeValid, DateTime.Now, false); var res = new List<string>(); foreach (X509Certificate2 s in certCollection) { if (search=="") res.Add(GetSubjectCN(s)); else if (s.SubjectName.Name.Contains(search)) res.Add(GetSubjectCN(s)); } return res; } finally { store.Close(); } }
C# Обработываем событие на Hotkey в приложениях WPF
Способ 1
Способ 2
public MainWindow() { InitializeComponent(); AddHandler(Keyboard.KeyDownEvent, (KeyEventHandler)HandleKeyDownEvent); } private void HandleKeyDownEvent(object sender, KeyEventArgs e) { if (e.Key == Key.Tab && (Keyboard.Modifiers & (ModifierKeys.Control | ModifierKeys.Shift)) == (ModifierKeys.Control | ModifierKeys.Shift)) { MessageBox.Show("CTRL + SHIFT + TAB trapped"); } if (e.Key == Key.Tab && (Keyboard.Modifiers & ModifierKeys.Control) == ModifierKeys.Control) { MessageBox.Show("CTRL + TAB trapped"); } }
Способ 2
<Window x:Class="WPFHotkey.MainWindow" xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" xmlns:local="clr-namespace:WPFHotkey" Title="MainWindow" Height="350" Width="525"> <Window.CommandBindings> <CommandBinding Command="local:MainWindow.Some_Command" Executed="Some_Executed"/> </Window.CommandBindings> <Window.InputBindings> <KeyBinding Key="F4" Modifiers="ALT" Command="local:MainWindow.Some_Command"/> </Window.InputBindings> <Grid> </Grid> </Window>
using System.Windows; using System.Windows.Input; namespace WPFHotkey { public partial class MainWindow : Window { public static RoutedCommand Some_Command = new RoutedCommand(); private void Some_Executed(object sender, ExecutedRoutedEventArgs e) { MessageBox.Show("Hi!"); } public MainWindow() { InitializeComponent(); } } }
c# проверяем xml по xsd схеме
using System; using System.Xml; using System.Xml.Schema; using System.IO; public class Sample { public static void Main() { XmlSchemaSet sc = new XmlSchemaSet(); sc.Add("", "books.xsd"); XmlReaderSettings settings = new XmlReaderSettings(); settings.ValidationType = ValidationType.Schema; settings.Schemas = sc; settings.ValidationEventHandler += new ValidationEventHandler (ValidationCallBack); XmlReader reader = XmlReader.Create("booksSchemaFail.xml", settings); while (reader.Read()); } private static void ValidationCallBack(object sender, ValidationEventArgs e) { Console.WriteLine("Validation Error: {0}", e.Message); } }
C# How to work with Excel. Simple example.
using Excel = Microsoft.Office.Interop.Excel; void CreateExcel() { Excel.Application xlApp; Excel.Workbook xlWorkBook; Excel.Worksheet xlWorkSheet; object misValue = System.Reflection.Missing.Value; xlApp = new Excel.Application();//new Excel.ApplicationClass(); xlWorkBook = xlApp.Workbooks.Add(misValue); xlWorkSheet = (Excel.Worksheet)xlWorkBook.Worksheets.get_Item(1); int i = 3; int j = 0; xlWorkSheet.Cells[1, 1] = "Protocol from " + DateTime.Now.ToShortDateString(); Excel.Range rg = (Excel.Range)xlWorkSheet.Cells[1, 1]; rg.EntireColumn.NumberFormat = "MM/DD/YYYY"; xlWorkSheet.Columns.AutoFit(); xlWorkBook.SaveAs("sdfgsdfg.xls", Excel.XlFileFormat.xlWorkbookNormal, misValue, misValue, misValue, misValue, Excel.XlSaveAsAccessMode.xlExclusive, Excel.XlSaveConflictResolution.xlLocalSessionChanges, misValue, misValue, misValue, misValue); xlWorkBook.Close(true, misValue, misValue); xlApp.Quit(); releaseObject(xlWorkSheet); releaseObject(xlWorkBook); releaseObject(xlApp); } private void releaseObject(object obj) { try { System.Runtime.InteropServices.Marshal.ReleaseComObject(obj); obj = null; } catch (Exception ex) { obj = null; } finally { GC.Collect(); } }
Subscribe to:
Posts (Atom)