Thursday 20 July 2017

C# Merge Splitted File

Hi all, here a very easy lwebcode free script to split file in C#

// calls example
...

int iSplittedFileSize = 1024 * 8;
MergeFile(@"C:\script\", iSplittedFileSize, @"C:\LwebCodeMergeFile\");

...

public static void MergeFile(string inputfoldername1, string SaveFileFolder)
        {
            string[] tmpfiles = Directory.GetFiles(inputfoldername1, "*.tmp");

            FileStream outPutFile = null;
            string PrevFileName = "";

            foreach (string tempFile in tmpfiles)
            {
                string fileName = Path.GetFileNameWithoutExtension(tempFile);
                string baseFileName = fileName.Substring(0, fileName.IndexOf(Convert.ToChar(".")));
                string extension = Path.GetExtension(fileName);

                if (!PrevFileName.Equals(baseFileName))
                {
                    if (outPutFile != null)
                    {
                        outPutFile.Flush();
                        outPutFile.Close();
                    }
                    outPutFile = new FileStream(SaveFileFolder + "\\" + baseFileName + extension, FileMode.OpenOrCreate, FileAccess.Write);

                }

                int bytesRead = 0;
                byte[] buffer = new byte[1024];
                FileStream inputTempFile = new FileStream(tempFile, FileMode.OpenOrCreate, FileAccess.Read);

                while ((bytesRead = inputTempFile.Read(buffer, 0, 1024)) > 0)
                    outPutFile.Write(buffer, 0, bytesRead);

                inputTempFile.Close();
               
                PrevFileName = baseFileName;

            }
        }


Hope you like it ;)

Friday 30 June 2017

C# Split File

Hi all, here a very easy lwebcode free script to split file in C#

// calls example
...

int iSplittedFileSize = 1024 * 8;
SplitFile(@"C:\script\lwebcode.bak", iSplittedFileSize, @"C:\script\");

...

public static void SplitFile(string inputFile, int chunkSize, string path)
{
    const int BUFFER_SIZE = 20 * 1024; //2 Kb for each file
    byte[] buffer = new byte[BUFFER_SIZE];

    string baseFileName = Path.GetFileNameWithoutExtension(inputFile);
    string Extension = Path.GetExtension(inputFile);

    using (Stream input = File.OpenRead(inputFile))
    {
        int index = 0;
        while (input.Position < input.Length)
        {
            string sFinalName = path + "\\" + baseFileName + "." + index.ToString().PadLeft(5, Convert.ToChar("0")) + Extension + ".tmp";
            using (Stream output = File.Create(sFinalName))
            {
                int remaining = chunkSize, bytesRead;
                while (remaining > 0 && (bytesRead = input.Read(buffer, 0,
                        Math.Min(remaining, BUFFER_SIZE))) > 0)
                {
                    output.Write(buffer, 0, bytesRead);
                    remaining -= bytesRead;
                }
            }
            index++;
            System.Threading.Thread.Sleep(500);
        }
    }
}


Hope you like it ;)

Wednesday 17 May 2017

C# Windows Service Debug

Hi all, here an easy example to make a debug on C# Windows Service ,
Implementation of Code founded on:

https://www.codeproject.com/Articles/14353/Creating-a-Basic-Windows-Service-in-C

With this useful example you create a Single *.exe file , but if you start the debugger
you can't debug the code with Visual Studio, Here is how to do:

First, make all step writed on CodeProject(in future reference I call this project "CodeProjectWS"), after you need to do this:

Right Clik on Current Solution -> Add -> New Project
On Project Type select Windows Consolle Application
select a name for new project, I choose :"LwebCodeWinDbg" -> Ok
Now in New Project "LwebCodeWinDbg" right-click the project name and go to:
[Add]->[Class]. Name the class "LwebCodeWindowsService.cs" and then hit OK.
Open class "LwebCodeWindowsService" and put this code:


namespace LwebCodeWinDbg
{
    public class LwebCodeWindowsService
    {
        public static void WindowsServiceStart()
        {
            System.IO.File.WriteAllText("c:\\LwebCodeTetsService.txt", DateTime.Now.ToString());
        }
    }
}

   
Now in New Project "LwebCodeWinDbg" open Program.cs and paste this code over Main Function:

namespace LwebCodeWinDbg
{
    class Program
    {
        static void Main(string[] args)
        {
            //put debug breakpoint on this istruction:
            LwebCodeWindowsService.WindowsServiceStart();
        }
    }
}


Now Open "CodeProjectWS" , right Clik on "Reference"-> Add Reference
in left pane select: "Solution"    -> Browse... and browse to "LwebCodeWinDbg" Project and then hit OK.
Open WindowsService.cs
and paste this code on OnStart Event:


protected override void OnStart(string[] args)
        {
            base.OnStart(args);
            LwebCodeWinDbg.LwebCodeWindowsService.WindowsServiceStart();

        }

with this istruction you can call the same function from Windows Service and Windows Consolle Application.
To debug the Windows service right click on "LwebCodeWinDbg" -> Set as Startup Project
F5 to start debug.

Hope it helps

Tuesday 18 April 2017

Javascript read JSON file and loop through records(optional with JQuery)

Hi all, here a very fast example on how to read json data from file in Javascript,
here the content of *.htm file:


<!DOCTYPE html>
<html>
<head>
    <title>LWebCode Javascript Read JSON from file</title>
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"></script>
    <script type="text/javascript" src="mydata.json"></script>
    <script>
        function ReadData()
        {
            var dataLstAz = JSON.parse(jsLstData);
            alert(jsLstData.length);
          
            for (var key=0, size=jsLstData.length; key<size; key++)
            {
                var obj = jsLstData[key];
                liout = obj.Name + ", " + obj.Age + "<br/>";
                $("#Output").append(liout);
            }
        }
      
        $( document ).ready(function() {
            ReadData();
        });
    </script>
</head>
<body>
    <div id="Output">
    </div>
</body>
</html>


Here the content of "mydata.json" file,
put this file in same folder of *.htm file, or change path in *.htm file:


jsLstData = '[{"Name":"MyName1","Age":"20"}, {"Name":"MyName2","Age":"22"}]';
//End of file "mydata.json"


Now simply open *.htm file in browser and you'll get this output:

MyName1 20
MyName2 22


That's it, hope it helps

Friday 31 March 2017

PowerShell Certificate Authority List, Loop, Filter Trought CA Certificate Authority

PowerShell Loop Trought CA Certificate Authority,
Hi all, here a quick and easy PowerShell script to list and filter all details of Installed Certificate Authority, if needed filter on line(s) given from certlst.txt file

function LWebCode_CALoop
{
    $MyDir = Split-Path -Path $MyInvocation.ScriptName
    $foutput = $MyDir + "\certlst.txt"
    Get-ChildItem -Recurse Cert: > $foutput
    $reader = [System.IO.File]::OpenText($foutput)
    while($null -ne ($line = $reader.ReadLine()))
    {

        echo $line
        /* optional filter on line(s)
        if($line -Match "Thumbprint")
        {
            echo $line
        }

        */
    }
}

LWebCode_CALoop


Do inside if block your preferred stuff

Saturday 18 March 2017

Send Mass Mail C#, PowerShell, Visual Basic, ASP, PHP, Curl, Node, Ruby, Python, Java, Go

Hi all, here some example and links to send Mass mail,
All this site can works with most common language: C#, PowerShell, Visual Basic, ASP, PHP, Curl, Node, Ruby, Python, Java, Go,
have lot of report and statistics graph, offering high level of personalization, and allow you to create your custom Marketing campaign


http://www.mailjet.com
Free Account has:
Month Max Limit: 6000
Day Max Limit: 200
Developement Support: SMTP , API


http://www.mailgun.com
Free Account has:
Month Max Limit: 10000
Day Max Limit: 400
Developement Support: SMTP , API


http://mailchimp.com
Free Account has:
Month Max Limit: 12000
Day Max Limit: ?
Developement Support: API

Monday 6 March 2017

PowerShell Encryption and Decryption, PS V 2.0 or Higher, Easy

Hi All, here's an easy example on how to encrypt or decrypt a string
this way IS NOT SURE because Key value is inside the *.ps1 file,
Another user can easily retrive your data.
This is only to understand basic of encryption in PowerShell



<#
LWEBCODE 
EASY POWERSHELL 2.0 or higher Encryption and Decryption
#>
$global:byteArr = New-Object Byte[] 32
$global:byteArr = [Byte[]](100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101, 100, 101)

function encrypt($content)
{
    $Password = $content | ConvertTo-SecureString -AsPlainText -Force
    $Password | ConvertFrom-SecureString -key $global:byteArr
    Write-Output "$Password"
    Write-Output "ENC END"
}

function decrypt($content)
{
    $User = "MyUserName"
    $MyCredential = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $User, ($content | ConvertTo-SecureString -Key $global:byteArr)
    $plainText = $MyCredential.GetNetworkCredential().Password

    Write-Output "Plain text: $plainText"
    Write-Output "DEC END"
}

encrypt("lwebcode is the best ;D ")

decrypt("76492d1116743f0423413b16050a5345MgB8AEoATwB4ADYAegBQAHcAaABhAGMATQBxAEwAWAA4AEI
ARgA2AE4ARwBlAFEAPQA9AHwAOQBjAGUAOABkAGQAYgAxADgAOABlAGMAMwA3AGEAYwA3AGQAMgA5AD
YAMgBmADQAZAA3ADIAYgAyADYANABiAGMAYQBjADIAMgA3AGEAYwA4AGQAYQBkAGUAOAAwADgANwAyA
DMANAAwADkAZQBhADcANgA5ADUANABhAGEAZgAwAGEAYwA3ADIANAA2ADcANQA3AGIAYQA4ADIAYgAx
ADUAOAAyADIANwBjADgAYgAxADgANAAyAGMAOAA5ADUAMQA0AGYAMQAxADgAOQAwADQAOQBiADQAZQA
yADMANwAyADgAZgBjAGMAOABkADkAYwAxAGMAMQBhADgANAA4AA==")