392 lines
15 KiB
C#
392 lines
15 KiB
C#
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel;
|
|
using System.Data;
|
|
using System.Drawing;
|
|
using System.Linq;
|
|
using System.Globalization;
|
|
using System.Text;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
using System.Drawing.Printing;
|
|
using ZedGraph;
|
|
using System.IO;
|
|
|
|
namespace GenesisDisplayTool
|
|
{
|
|
public static class Files
|
|
{
|
|
#region Open files/ save to file
|
|
public static Boolean OpenText(out String textToGet, String fullpath, CultureInfo culture)
|
|
{
|
|
Boolean OpeningOk = false;
|
|
textToGet = String.Empty;
|
|
|
|
try
|
|
{
|
|
textToGet = File.ReadAllText(fullpath);
|
|
OpeningOk = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), " Files.OpenText() failed", $"0x{ex.HResult.ToString("X", culture)}", String.Empty);
|
|
}
|
|
|
|
return OpeningOk;
|
|
}
|
|
public static Boolean SaveText(String textToWrite, String fullpath, CultureInfo culture)
|
|
{
|
|
Boolean SavingOK = false;
|
|
|
|
try
|
|
{
|
|
File.WriteAllText(fullpath, textToWrite);
|
|
SavingOK = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), " Files.SaveText() failed", $"0x{ex.HResult.ToString("X", culture)}", String.Empty);
|
|
|
|
}
|
|
|
|
return SavingOK;
|
|
}
|
|
public static Boolean SaveImage(ZedGraphControl graph, String fullpath, CultureInfo culture)
|
|
{
|
|
Boolean SavingOK = false;
|
|
|
|
try
|
|
{
|
|
graph.MasterPane.GetImage(1080, 640, 600, true).Save(fullpath);
|
|
SavingOK = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), " Files.SaveToImage() failed", $"0x{ex.HResult.ToString("X", culture)}", String.Empty);
|
|
}
|
|
|
|
return SavingOK;
|
|
}
|
|
public static Boolean SavePdf(PdfSharp.Pdf.PdfDocument document, String fullpath)
|
|
{
|
|
Boolean SavingOK = false;
|
|
|
|
try
|
|
{
|
|
Task.Run(() =>
|
|
{
|
|
document.Save(@fullpath);
|
|
System.Diagnostics.Process.Start(@fullpath);
|
|
});
|
|
SavingOK = true;
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), " Files.SavePdf() failed", "0x" + ex.HResult.ToString("X", GENESIS_DISPLAY_TOOL._GENESIS_DISPLAY_TOOL.GetGlobalCulture()), String.Empty);
|
|
}
|
|
|
|
return SavingOK;
|
|
}
|
|
#endregion
|
|
#region File dialogs
|
|
public static Boolean SaveFiledialogPdf(String itemname, ref String fullpath, String initialDirectory)
|
|
{
|
|
#region Definitions and initializations
|
|
String FullFilename = String.Empty;
|
|
Boolean DialogOK = false;
|
|
#endregion
|
|
#region Everything else
|
|
try
|
|
{
|
|
#region Set Filename
|
|
FullFilename = $"{DateTime.Now.ToString(Formats.PdfDateTimeString)}GENESIS_{itemname}.pdf";
|
|
#endregion
|
|
#region SaveFileDialog
|
|
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
|
|
saveFileDialog1.Filter = "document (*.pdf)|*.pdf";
|
|
saveFileDialog1.FilterIndex = 2;
|
|
saveFileDialog1.RestoreDirectory = true;
|
|
saveFileDialog1.FileName = FullFilename;
|
|
if (Directory.Exists(initialDirectory))
|
|
saveFileDialog1.InitialDirectory = initialDirectory;
|
|
#endregion
|
|
#region Save to file
|
|
if (OpenFileDialog.Equals(saveFileDialog1.ShowDialog(), DialogResult.OK))
|
|
{
|
|
DialogOK = true;
|
|
fullpath = Path.GetFullPath(saveFileDialog1.FileName);
|
|
|
|
}
|
|
#endregion
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), " Files.SaveFiledialogPdf() failed", $"0x{ex.HResult.ToString("X", GENESIS_DISPLAY_TOOL._GENESIS_DISPLAY_TOOL.GetGlobalCulture())}", String.Empty);
|
|
}
|
|
#endregion
|
|
|
|
return DialogOK;
|
|
}
|
|
public static Boolean SaveFiledialogPng(String itemname, out String fullpath, String initialDirectory)
|
|
{
|
|
#region Definitions and initializations
|
|
String FullFilename = String.Empty;
|
|
Boolean DialogOK = false;
|
|
fullpath = String.Empty;
|
|
#endregion
|
|
#region Everything else
|
|
try
|
|
{
|
|
#region Set Filename
|
|
FullFilename = $"{ DateTime.Now.ToString(Formats.ImageFileDateTimeString)}GENESIS_{itemname}.png";
|
|
#endregion
|
|
#region SaveFileDialog
|
|
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
|
|
saveFileDialog1.Filter = "image (*.png)|*.png";
|
|
saveFileDialog1.FilterIndex = 2;
|
|
saveFileDialog1.RestoreDirectory = true;
|
|
saveFileDialog1.FileName = FullFilename;
|
|
if (Directory.Exists(initialDirectory))
|
|
saveFileDialog1.InitialDirectory = initialDirectory;
|
|
#endregion
|
|
#region Save to file
|
|
if (OpenFileDialog.Equals(saveFileDialog1.ShowDialog(), DialogResult.OK))
|
|
{
|
|
DialogOK = true;
|
|
fullpath = Path.GetFullPath(saveFileDialog1.FileName);
|
|
}
|
|
saveFileDialog1.Dispose();
|
|
#endregion
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), " Files.SaveFiledialogPng() failed", $"0x{ex.HResult.ToString("X", GENESIS_DISPLAY_TOOL._GENESIS_DISPLAY_TOOL.GetGlobalCulture())}", String.Empty);
|
|
|
|
}
|
|
#endregion
|
|
|
|
return DialogOK;
|
|
}
|
|
public static Boolean SaveFiledialogCsv(String itemname, out String fullpath, String initialDirectory)
|
|
{
|
|
#region Definitions and initializations
|
|
String FullFilename = String.Empty;
|
|
Boolean DialogOK = false;
|
|
fullpath = String.Empty;
|
|
#endregion
|
|
#region Everything else
|
|
try
|
|
{
|
|
#region Set Filename
|
|
FullFilename = DateTime.Now.ToString(Formats.CsvFileDateTimeString) +"GENESIS_"+ itemname + ".csv";
|
|
#endregion
|
|
#region SaveFileDialog
|
|
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
|
|
saveFileDialog1.Filter = "comma separated values (*.csv)|*.csv";
|
|
saveFileDialog1.FilterIndex = 2;
|
|
saveFileDialog1.RestoreDirectory = true;
|
|
saveFileDialog1.FileName = FullFilename;
|
|
if (Directory.Exists(initialDirectory))
|
|
saveFileDialog1.InitialDirectory = initialDirectory;
|
|
#endregion
|
|
#region Save to file
|
|
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
|
|
{
|
|
DialogOK = true;
|
|
fullpath = Path.GetFullPath(saveFileDialog1.FileName);
|
|
}
|
|
#endregion
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), " Files.SaveFiledialogCsv() failed", "0x" + ex.HResult.ToString("X", GENESIS_DISPLAY_TOOL._GENESIS_DISPLAY_TOOL.GetGlobalCulture()), String.Empty);
|
|
}
|
|
#endregion
|
|
|
|
return DialogOK;
|
|
}
|
|
public static Boolean SaveFiledialogConversion(String itemname, ref String fullpath, String initialDirectory)
|
|
{
|
|
#region Definitions and initializations
|
|
String FullFilename = String.Empty;
|
|
Boolean DialogOK = false;
|
|
#endregion
|
|
#region Everything else
|
|
try
|
|
{
|
|
#region Set Filename
|
|
FullFilename = itemname + ".csv";
|
|
#endregion
|
|
#region SaveFileDialog
|
|
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
|
|
saveFileDialog1.Filter = "comma separated values (*.csv)|*.csv";
|
|
saveFileDialog1.FilterIndex = 2;
|
|
saveFileDialog1.RestoreDirectory = true;
|
|
saveFileDialog1.FileName = FullFilename;
|
|
if (Directory.Exists(initialDirectory))
|
|
saveFileDialog1.InitialDirectory = initialDirectory;
|
|
#endregion
|
|
#region Save to file
|
|
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
|
|
{
|
|
DialogOK = true;
|
|
fullpath = Path.GetFullPath(saveFileDialog1.FileName);
|
|
}
|
|
#endregion
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), " Files.SaveFiledialogConversion() failed", "0x" + ex.HResult.ToString("X", GENESIS_DISPLAY_TOOL._GENESIS_DISPLAY_TOOL.GetGlobalCulture()), String.Empty);
|
|
}
|
|
#endregion
|
|
|
|
return DialogOK;
|
|
}
|
|
public static Boolean SaveFiledialogTxt(String itemname, out String fullpath, String initialDirectory)
|
|
{
|
|
#region Definitions and initializations
|
|
String FullFilename = String.Empty;
|
|
fullpath = String.Empty;
|
|
Boolean DialogOK = false;
|
|
#endregion
|
|
#region Everything else
|
|
try
|
|
{
|
|
#region Set Filename
|
|
FullFilename = $"{DateTime.Now.ToString(Formats.TxtFileDateTimeString)}GENESIS_{itemname}.txt";
|
|
#endregion
|
|
#region SaveFileDialog
|
|
SaveFileDialog saveFileDialog1 = new SaveFileDialog();
|
|
saveFileDialog1.Filter = "text files (*.txt)|*.txt";
|
|
saveFileDialog1.FilterIndex = 2;
|
|
saveFileDialog1.RestoreDirectory = true;
|
|
saveFileDialog1.FileName = FullFilename;
|
|
if (Directory.Exists(initialDirectory))
|
|
saveFileDialog1.InitialDirectory = initialDirectory;
|
|
#endregion
|
|
#region Save to file
|
|
if (saveFileDialog1.ShowDialog() == DialogResult.OK)
|
|
{
|
|
DialogOK = true;
|
|
fullpath = Path.GetFullPath(saveFileDialog1.FileName);
|
|
}
|
|
#endregion
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), " Files.SaveFiledialogTxt() failed", $"0x{ex.HResult.ToString("X")}", String.Empty);
|
|
}
|
|
#endregion
|
|
|
|
return DialogOK;
|
|
}
|
|
public static Boolean OpenFileDialogText(out String fullpath, String initialDirectory)
|
|
{
|
|
#region Definitions and initializations
|
|
String Filename = String.Empty;
|
|
String FileContent = String.Empty;
|
|
Boolean DialogOK = false;
|
|
fullpath = String.Empty;
|
|
#endregion
|
|
#region File dialog
|
|
OpenFileDialog openFileDialog1 = new OpenFileDialog();
|
|
|
|
openFileDialog1.Filter = "text files (*.txt)|*.txt";
|
|
openFileDialog1.FilterIndex = 2;
|
|
openFileDialog1.RestoreDirectory = true;
|
|
openFileDialog1.FileName = Filename;
|
|
if (Directory.Exists(initialDirectory))
|
|
openFileDialog1.InitialDirectory = initialDirectory;
|
|
#endregion
|
|
#region Open file
|
|
try
|
|
{
|
|
// -- open file --
|
|
if (OpenFileDialog.Equals(openFileDialog1.ShowDialog(), DialogResult.OK))
|
|
{
|
|
#region Get PATH
|
|
fullpath = Path.GetFullPath(openFileDialog1.FileName);
|
|
DialogOK = true;
|
|
#endregion
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), "Files.OpenFileDialogText() failed", $"0x{ex.HResult.ToString("X", GENESIS_DISPLAY_TOOL._GENESIS_DISPLAY_TOOL.GetGlobalCulture())}", String.Empty);
|
|
}
|
|
#endregion
|
|
#region Return
|
|
return DialogOK;
|
|
#endregion
|
|
}
|
|
public static Boolean OpenFileDialogJson(ref String fullpath, String initialDirectory)
|
|
{
|
|
#region Definitions and initializations
|
|
String Filename = String.Empty;
|
|
String FileContent = String.Empty;
|
|
Boolean DialogOK = false;
|
|
#endregion
|
|
#region File dialog
|
|
OpenFileDialog openFileDialog1 = new OpenFileDialog();
|
|
openFileDialog1.Filter = "json files (*.json)|*.json";
|
|
openFileDialog1.FilterIndex = 2;
|
|
openFileDialog1.RestoreDirectory = true;
|
|
openFileDialog1.FileName = Filename;
|
|
if (Directory.Exists(initialDirectory))
|
|
openFileDialog1.InitialDirectory = initialDirectory;
|
|
#endregion
|
|
#region Open file
|
|
// -- open file --
|
|
if (OpenFileDialog.Equals(openFileDialog1.ShowDialog(), DialogResult.OK))
|
|
{
|
|
#region Get PATH
|
|
fullpath = Path.GetFullPath(openFileDialog1.FileName);
|
|
DialogOK = true;
|
|
#endregion
|
|
}
|
|
#endregion
|
|
#region Return
|
|
return DialogOK;
|
|
#endregion
|
|
}
|
|
public static Boolean OpenFileDialogCsv(ref String fullpath, String initialDirectory)
|
|
{
|
|
#region Definitions and initializations
|
|
String Filename = String.Empty;
|
|
String FileContent = String.Empty;
|
|
Boolean DialogOK = false;
|
|
#endregion
|
|
#region File dialog
|
|
OpenFileDialog openFileDialog1 = new OpenFileDialog();
|
|
openFileDialog1.Filter = "Comma seperated values (*.csv)|*.csv";
|
|
openFileDialog1.FilterIndex = 2;
|
|
openFileDialog1.RestoreDirectory = true;
|
|
openFileDialog1.FileName = Filename;
|
|
if (Directory.Exists(initialDirectory))
|
|
openFileDialog1.InitialDirectory = initialDirectory;
|
|
#endregion
|
|
#region Open file
|
|
try
|
|
{
|
|
// -- open file --
|
|
if (openFileDialog1.ShowDialog() == DialogResult.OK)
|
|
{
|
|
#region Get PATH
|
|
fullpath = Path.GetFullPath(openFileDialog1.FileName);
|
|
DialogOK = true;
|
|
#endregion
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
ErrorLog.Log(LogErrReason.Exception(), "Files.OpenFileDialogCsv() failed", "0x" + ex.HResult.ToString("X"), String.Empty);
|
|
}
|
|
#endregion
|
|
#region Return
|
|
return DialogOK;
|
|
#endregion
|
|
}
|
|
#endregion
|
|
}
|
|
} |