Buscar contenidos

viernes, 15 de diciembre de 2017

c# Escribir en EventLog

  static string EventLogName = "Name";

        private static bool CheckSourceExists(string source)
        {
            if (EventLog.SourceExists(source))
            {
                EventLog evLog = new EventLog { Source = source };

                if (evLog.Log != EventLogName)
                {
                    EventLog.DeleteEventSource(source);
                }
            }

            if (!EventLog.SourceExists(source))
            {
                EventLog.CreateEventSource(source, EventLogName);

                EventLog.WriteEntry(source, String.Format("Event Log Created '{0}'/'{1}'", EventLogName, source), EventLogEntryType.Information);
            }

            return EventLog.SourceExists(source);
        }

        private static void WriteEventToMyLog(string source, string text, EventLogEntryType type)
        {
            if (CheckSourceExists(source))
            {
                EventLog.WriteEntry(source, text, type);
            }
        }

      

lunes, 11 de diciembre de 2017

C# Imprimir PDF a una impresora en específico




string a = System.Environment.MachineName;

Process proc = new Process();
//"C:\\Program Files (x86)\\Adobe\\Acrobat Reader DC\\Reader\\AcroRd32.exe"
proc.StartInfo.FileName = adobePath;

//FilePath: c://test.pdf
proc.StartInfo.Arguments = @" /t /h " + "\"" + FilePath + "\"" + " " + "\"" + "Bullzip PDF Printer" + "\"";
proc.StartInfo.UseShellExecute = true;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
Thread.Sleep(1000);
proc.WaitForInputIdle();




public static List<string> GetPrinterList()
{
    List<string> l = new List<string>();

    foreach (string printer in System.Drawing.Printing.PrinterSettings.InstalledPrinters)
    {
        l.Add(printer);
    }

    return l;
}

public static string GetDefaultPrinter()
{
    PrinterSettings settings = new PrinterSettings();

    return (settings.PrinterName);
}
 

miércoles, 8 de noviembre de 2017

Encrypt/Decrypt using Self-signed Certificates

Link artículo




        static void Main(string[] args)
        {
            try
            {
                X509Certificate2 myCert =
                   LoadCertificate(StoreLocation.CurrentUser,
                   "CN=test");

                string myText = "This is the text I wish to encrypt";
                Console.WriteLine("UNENCRYPTED: " + myText);

                string encrypted = Encrypt(myCert, myText);

                Console.WriteLine("ENCRYPTED: " + encrypted);

                string decrypted = Decrypt(myCert, encrypted);

                Console.WriteLine("DECRYPTED: " + decrypted);
            }
            catch (Exception e)
            {
                Console.WriteLine("EXCEPTION: {0}", e.Message);
            }

        }

        public static X509Certificate2
   LoadCertificate(StoreLocation storeLocation,
   string certificateName)
        {
            X509Store store = new X509Store(storeLocation);
            store.Open(OpenFlags.ReadOnly);
            X509Certificate2Collection certCollection =
               store.Certificates;
            X509Certificate2 cert =
               certCollection.Cast<X509Certificate2>().FirstOrDefault
               (c => c.Subject == certificateName);
            if (cert == null)
                Console.WriteLine("NO Certificate named " +
                   certificateName + " was found in your certificate store");
            store.Close();
            return cert;
        }


        private static string Encrypt(X509Certificate2 x509, string stringToEncrypt)
        {
            if (x509 == null || string.IsNullOrEmpty(stringToEncrypt))
                throw new Exception("A x509 certificate and string for encryption must be provided");

            RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)x509.PublicKey.Key;
            byte[] bytestoEncrypt = ASCIIEncoding.ASCII.GetBytes(stringToEncrypt);
            byte[] encryptedBytes = rsa.Encrypt(bytestoEncrypt, false);
            return Convert.ToBase64String(encryptedBytes);
        }

        private static string Decrypt(X509Certificate2 x509, string stringTodecrypt)
        {
            if (x509 == null || string.IsNullOrEmpty(stringTodecrypt))
                throw new Exception("A x509 certificate and string for decryption must be provided");

            if (!x509.HasPrivateKey)
                throw new Exception("x509 certicate does not contain a private key for decryption");

            RSACryptoServiceProvider rsa = (RSACryptoServiceProvider)x509.PrivateKey;
            byte[] bytestodecrypt = Convert.FromBase64String(stringTodecrypt);
            byte[] plainbytes = rsa.Decrypt(bytestodecrypt, false);
            System.Text.ASCIIEncoding enc = new System.Text.ASCIIEncoding();
            return enc.GetString(plainbytes);
        }