Hi im trying to convert a file to a base64String and then store that string in a text file. What i am trying to create is a Windows Service - As soon as a file is created in a certain folder.The Service creates a Log file to say that a file has been added to the folder put then it should convert that file into a base64String and store that string in a text file.
EDIT 1:
Im trying to convert a file into a base64String.The code below successfully registers a new file being added to a folder. What im trying to do now is convert that file that has just been added to a base64String and store that string in a separate file. To code to convert to base64String is in the method
ConvertToBase64(), That method is being called inside
FileSystemWatcher1_Created(). What i have tried can be seen below
I Want to be able convert any file to a base64String that is added to certain folder
My Code
using System;
using System.IO;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Threading.Tasks;
using System.Configuration;
using System.Threading;
using System.Reflection;
namespace WindowsServiceFolderWatcher
{
public partial class Service1 : ServiceBase
{
string WatchPath1 = ConfigurationManager.AppSettings["WatchPath1"];
public Service1()
{
InitializeComponent();
fileSystemWatcher1.Created += fileSystemWatcher1_Created;
}
internal void TestStartupAndStop(string[] args)
{
this.OnStart(args);
Console.ReadLine();
this.OnStop();
}
FileSystemWatcher myWatcher = new FileSystemWatcher();
protected override void OnStart(string[] args)
{
try
{
fileSystemWatcher1.Path = WatchPath1;
}
catch (Exception ex)
{
throw ex;
}
}
protected override void OnStop()
{
try
{
Create_ServiceStopTextFile();
}
catch (Exception ex)
{
throw ex;
}
}
void fileSystemWatcher1_Created(object sender, System.IO.FileSystemEventArgs e)
{
try
{
Thread.Sleep(7000);
if(CheckFileExistance(WatchPath1, e.Name))
{
CreateTextFile(WatchPath1, e.Name);
ConvertToBase64();
}
}
catch(Exception ex)
{
throw ex;
}
}
private bool CheckFileExistance(string FullPath, string FileName)
{
bool IsFileExist = false;
DirectoryInfo dir = new DirectoryInfo(FullPath);
if (!dir.Exists)
IsFileExist = false;
else
{
string FileFullPath = Path.Combine(FullPath, FileName);
if (File.Exists(FileFullPath))
IsFileExist = true;
}
return IsFileExist;
}
private void CreateTextFile(string FullPath, string FileName)
{
StreamWriter SW;
if (!File.Exists(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txtStatus_" + DateTime.Now.ToString("MMddyyyy") + ".txt")))
{
SW = File.CreateText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txtStatus_" + DateTime.Now.ToString("MMddyyyy") + ".txt"));
SW.Close();
}
using (SW = File.AppendText(Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), "txtStatus_" + DateTime.Now.ToString("MMddyyyy") + ".txt")))
{
SW.WriteLine("File Created with Name: " + FileName + " at this time: " + DateTime.Now.ToString("HH:mm , MM/dd/yyyy"));
SW.Close();
}
}
public static void ConvertToBase64()
{
using (System.IO.Stream s = new System.IO.FileStream(@"C:\\Users\\username\\Desktop\\Ball.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
var bytes = StreamHelper.ReadToEnd(s);
var string64 = Convert.ToBase64String(bytes);
File.WriteAllBytes(@"C:\\Users\\username\\Desktop\\Base64\\Base.txt", Convert.FromBase64String(string64));
}
}
public static void Create_ServiceStopTextFile()
{
string Destination = "C:\\Users\\username\\Desktop\\FileWatcherWinService";
StreamWriter SW;
if (Directory.Exists(Destination))
{
Destination = System.IO.Path.Combine(Destination, "txtServiceStop_" + DateTime.Now.ToString("yyyyMMdd") + ".txt");
if (!File.Exists(Destination))
{
SW = File.CreateText(Destination);
SW.Close();
}
}
using (SW = File.AppendText(Destination))
{
SW.Write("\r\n\n");
SW.WriteLine("Service Stopped at: " + DateTime.Now.ToString("dd-MM-yyyy H:mm:ss"));
SW.Close();
}
}
public static void Copy_Excelfile_And_Paste_at_anotherloaction_OnServiceStart()
{
try
{
string source = "C:\\Users\\username\\Desktop\\Documents";
string Destination = "C:\\Users\\username\\Desktop\\Destination";
string filename = string.Empty;
if (!(Directory.Exists(Destination) && Directory.Exists(source)))
return;
string[] Templateexcelfile = Directory.GetFiles(source);
foreach (string file in Templateexcelfile)
{
if (file.Contains("Excel"))
{
filename = System.IO.Path.GetFileName(file);
Destination = System.IO.Path.Combine(Destination, filename.Replace(".xlsx", DateTime.Now.ToString("yyyyMMdd")) + ".xlsx");
System.IO.File.Copy(file, Destination, true);
}
}
}
catch (Exception ex)
{
Create_ServiceStopTextFile();
}
}
internal void DebugMode()
{
this.OnStart(null);
}
}
}
What I have tried:
using (System.IO.Stream s = new System.IO.FileStream(@"C:\\Users\\username\\Desktop\\Ball.jpg", System.IO.FileMode.Open, System.IO.FileAccess.Read))
{
var bytes = StreamHelper.ReadToEnd(s);
var string64 = Convert.ToBase64String(bytes);
File.WriteAllBytes(@"C:\\Users\\username\\Desktop\\Base64\\Base.txt", Convert.FromBase64String(string64));
}