Skip to content

Instantly share code, notes, and snippets.

let date = new Date();
let today = new Date(date.getFullYear(), date.getMonth(), date.getDate(), 0, 0, 0);
console.log(today.toISOString());
let date = new Date();
let today = ("0" + date.getDate()).slice(-2) + "/" + ("0"+(date.getMonth()+1)).slice(-2);
class Program
{
private static readonly HttpClient _httpClient = new HttpClient();
static void Main(string[] args)
{
var url = "";
byte[] fileBytes = null;
var httpMessage = new HttpRequestMessage(HttpMethod.Get, url);
public static TextFieldParser GetTextFieldParser(string source)
{
if (!source.IsUrl())
return new TextFieldParser(source);
var file = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "files", $"{DateTime.Now:yyyyMMddHHmmssfff}", Path.GetFileName(source));
var path = Path.GetDirectoryName(file);
if (!Directory.Exists(path))
Directory.CreateDirectory(path);
@lfgrando
lfgrando / XmlSerializerFromClass.cs
Created August 31, 2018 15:16 — forked from souzagustavo/XmlSerializerFromClass.cs
C# - XmlSerialize from class and remove default namespaces
XmlSerializer serializer = new XmlSerializer(typeof(MetadataFile));
MemoryStream memoryStream = new MemoryStream();
//Configuration remove tag omit-xml-declaration
XmlWriterSettings settings = new XmlWriterSettings();
settings.OmitXmlDeclaration = true;
XmlWriter writer = XmlWriter.Create(memoryStream, settings);
//Remove namespace
StringWriter stringWriter = new StringWriter();
@lfgrando
lfgrando / RecoverDeadLetterQueuesGeneric.cs
Created August 27, 2018 14:28
RecoverDeadLetterQueuesGeneric
using Microsoft.ServiceBus.Messaging;
using System;
using System.Configuration;
namespace RecoverDeadLetterQueuesGeneric
{
class Program
{
static void Main(string[] args)
{
@lfgrando
lfgrando / RenameToken.cs
Created August 21, 2018 15:54
How to rename a JToken
private void RenameToken(JToken token, string newName)
{
var parent = token.Parent;
if (parent == null)
throw new InvalidOperationException("The parent is missing.");
var newToken = new JProperty(newName, token);
parent.Replace(newToken);
}
JToken cpf = serializedCustomer["documents"]?["cpf"];
@lfgrando
lfgrando / Regex.cs
Last active August 31, 2018 15:07
Many regex helpers
var content = "{ \"name\": \"Leandro Grando\" }";
// Remove all spaces and new lines from object
var spacelessContent = Regex.Replace(content, @"\s+", string.Empty);
// Minify json string
var minifiedJson = Regex.Replace(content, "(\"(?:[^\"\\\\]|\\\\.)*\")|\\s+", "$1");
// Negate regex - not tested in c#
var negated = ^((?!gmail.com).)*$
@lfgrando
lfgrando / SetApiMaxContentLengthUpload.xml
Last active August 31, 2018 14:57
API Upload Limit Size to 1gb at Web.config. It seems it doesn't work at all IIS's. Looks like not working at IIS Express.
<system.webServer>
<security>
<requestFiltering>
<requestLimits maxAllowedContentLength="1073741824" />
</requestFiltering>
</security>
</system.webServer>
@lfgrando
lfgrando / ReplaceMatchToken.cs
Last active July 20, 2018 18:15
Replace custom matched token on JToken
public static class JTokenExtensions
{
private JToken LookupPreviousData(string value, JToken resourceToken)
{
if (!resourceToken.HasValues)
return null;
if (!Regex.Match(value, ProjectionMatch).Success)
return null;