Skip to content

Instantly share code, notes, and snippets.

@lukeawyatt
Last active January 8, 2021 20:23
Show Gist options
  • Save lukeawyatt/8044d57a2e30a88fcece45c1a737e755 to your computer and use it in GitHub Desktop.
Save lukeawyatt/8044d57a2e30a88fcece45c1a737e755 to your computer and use it in GitHub Desktop.
Lang: C#
using System;
using System.IO;
using CsvHelper;
namespace CsvHelperTest
{
// THIS USES CsvHelper FROM NUGET
public class Program
{
public static void Main(string[] args)
{
using (StreamReader reader = new StreamReader(AppDomain.CurrentDomain.BaseDirectory + @"\example.csv"))
{
var csv = new CsvReader(reader);
csv.Configuration.DetectColumnCountChanges = true;
csv.Configuration.HasHeaderRecord = true;
csv.Configuration.TrimFields = true;
csv.Configuration.IgnoreBlankLines = true;
csv.Configuration.SkipEmptyRecords = true;
while (csv.Read())
{
string x = csv.GetField<int>("test1") + ", ";
x += csv.GetField<string>("test 2") + ", ";
x += csv.GetField<string>("test,3") + ", ";
Console.WriteLine(x);
}
reader.Close();
}
Console.ReadLine();
}
}
}
public sealed class Wallpaper
{
Wallpaper() { }
const int SPI_SETDESKWALLPAPER = 20;
const int SPIF_UPDATEINIFILE = 0x01;
const int SPIF_SENDWININICHANGE = 0x02;
[DllImport("user32.dll", CharSet = CharSet.Auto)]
static extern int SystemParametersInfo(int uAction, int uParam, string lpvParam, int fuWinIni);
public enum Style : int
{
Tiled,
Centered,
Stretched
}
public static void Set(Uri uri, Style style)
{
System.IO.Stream s = new System.Net.WebClient().OpenRead(uri.ToString());
System.Drawing.Image img = System.Drawing.Image.FromStream(s);
string tempPath = Path.Combine(Path.GetTempPath(), "wallpaper.bmp");
img.Save(tempPath, System.Drawing.Imaging.ImageFormat.Bmp);
RegistryKey key = Registry.CurrentUser.OpenSubKey(@"Control Panel\Desktop", true);
if (style == Style.Stretched)
{
key.SetValue(@"WallpaperStyle", 2.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Centered)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 0.ToString());
}
if (style == Style.Tiled)
{
key.SetValue(@"WallpaperStyle", 1.ToString());
key.SetValue(@"TileWallpaper", 1.ToString());
}
SystemParametersInfo(SPI_SETDESKWALLPAPER, 0, tempPath, SPIF_UPDATEINIFILE | SPIF_SENDWININICHANGE);
}
}
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Mail;
using System.Text;
using System.Threading.Tasks;
namespace smtp_relay_test
{
class Program
{
static void Main(string[] args)
{
SmtpClient client = new SmtpClient
{
Port = 587,
Host = "smtp.gmail.com",
EnableSsl = true,
Timeout = 10000,
DeliveryMethod = SmtpDeliveryMethod.Network,
UseDefaultCredentials = false,
Credentials = new System.Net.NetworkCredential("relay@gmail.com", "pw")
};
var msg = new MailMessage
{
From = new MailAddress("relay@gmail.com"),
Subject = "Test Subject",
Body = "Test Body",
IsBodyHtml = true,
BodyEncoding = Encoding.UTF8,
DeliveryNotificationOptions = DeliveryNotificationOptions.OnFailure
};
foreach (string address in "test@gmail.com".Split(new char[] { ',', ';' }, StringSplitOptions.RemoveEmptyEntries))
{
msg.To.Add(address.Trim());
}
client.Send(msg);
}
}
}
public class PDF
{
public static Boolean PrintPDFs(string pdfFileName)
{
try
{
Process proc = new Process();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
proc.StartInfo.Verb = "print";
proc.StartInfo.FileName = @"C:\Program Files (x86)\Adobe\Reader 11.0\Reader\AcroRd32.exe";
//proc.StartInfo.Arguments = String.Format(@"/s /h /p {0}", pdfFileName);
proc.StartInfo.Arguments = String.Format("/s /h /t \"{0}\" \"{1}\" \"{2}\" \"{3}\"", pdfFileName, @" lobalcellular.com\Dell Laser MFP 1815", "PrintQueue.inf", "USB001");
proc.StartInfo.UseShellExecute = false;
proc.StartInfo.CreateNoWindow = true;
proc.Start();
proc.StartInfo.WindowStyle = ProcessWindowStyle.Hidden;
if (proc.HasExited == false)
{
proc.WaitForExit(10000);
}
proc.EnableRaisingEvents = true;
proc.Close();
KillAdobe("AcroRd32");
return true;
}
catch
{
return false;
}
}
private static bool KillAdobe(string name)
{
foreach (Process clsProcess in Process.GetProcesses().Where(
clsProcess => clsProcess.ProcessName.StartsWith(name)))
{
clsProcess.Kill();
return true;
}
return false;
}
}
/** GENERIC PARSING **/
private string FormatXMLChars(string value)
{
if (string.IsNullOrEmpty(xml))
return xml;
return xml.Replace("'", "&apos;")
.Replace("\"", "&quot;")
.Replace("&", "&amp;")
.Replace(">", "&gt;")
.Replace("<", "&lt;");
}
/** .NET 4.0+ **/
private void TEST()
{
// REPLACE BAD XML CHARS
string content = "\v\f\0";
MessageBox.Show(IsValidXmlString(content).ToString() + ": " + content);
content = XmlConvert.EncodeName(content);
MessageBox.Show(IsValidXmlString(content).ToString() + ": " + content);
// REMOVE BAD XML CHARS
content = "\v\f\0";
MessageBox.Show(IsValidXmlString(content).ToString() + ": " + content);
content = RemoveInvalidXmlChars(content);
MessageBox.Show(IsValidXmlString(content).ToString() + ": " + content);
}
/** .NET 4.0+ **/
private string RemoveInvalidXmlChars(string text)
{
var validXmlChars = text.Where(ch => XmlConvert.IsXmlChar(ch)).ToArray();
return new string(validXmlChars);
}
/** .NET 4.0+ **/
private bool IsValidXmlString(string text)
{
try
{
XmlConvert.VerifyXmlChars(text);
return true;
}
catch
{
return false;
}
}
static private string XSLTransForm(XmlDocument doc, string xslPath, string[] param)
{
string text = string.Empty;
try
{
XmlDocument stylesheet = new XmlDocument();
stylesheet.Load(xslPath);
XslCompiledTransform transform = new XslCompiledTransform();
transform.Load(stylesheet);
XsltArgumentList xsltArgumentList = null;
if (param != null)
{
xsltArgumentList = new XsltArgumentList();
for (int i = 0; i < param.Length; i = i + 2)
xsltArgumentList.AddParam(param[i], "", param[i + 1]);
}
MemoryStream stream = new MemoryStream();
transform.Transform(doc, xsltArgumentList, stream);
stream.Position = 0;
StreamReader reader = new StreamReader(stream);
text = reader.ReadToEnd();
stream.Close();
reader.Close();
}
catch (Exception e)
{
// SUPPRESS FOR THE TIME BEING
}
return text;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment