Last active
April 25, 2018 13:16
-
-
Save amenayach/ee866990936658370d77f54bb56f4b24 to your computer and use it in GitHub Desktop.
A C# base64 file converter.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.IO; | |
using System.Linq; | |
public static class Base64FileConverter | |
{ | |
public static string ToBase64(byte[] bytes) | |
{ | |
if (bytes == null || bytes.Length == 0) | |
{ | |
return string.Empty; | |
} | |
return Convert.ToBase64String(bytes); | |
} | |
public static string ToBase64(this string filename) | |
{ | |
var filepath = filename.Contains('\\') ? filename : (Path.Combine(Directory.GetCurrentDirectory(), filename)); | |
if (!File.Exists(filepath)) | |
{ | |
return null; | |
} | |
return Convert.ToBase64String(File.ReadAllBytes(filepath)); | |
} | |
public static byte[] ToBytes(this string base54String) | |
{ | |
if (string.IsNullOrWhiteSpace(base54String)) | |
{ | |
return null; | |
} | |
return Convert.FromBase64String(base54String); | |
} | |
public static bool ToFile(this string base54String, string filename) | |
{ | |
if (string.IsNullOrWhiteSpace(base54String)) | |
{ | |
return false; | |
} | |
var bytes = Convert.FromBase64String(base54String); | |
var filepath = filename.Contains('\\') ? filename : (Path.Combine(Directory.GetCurrentDirectory(), filename)); | |
File.WriteAllBytes(filepath, bytes); | |
return true; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment