127 lines
3.6 KiB
C#
127 lines
3.6 KiB
C#
using Gremlin_BlazorServer.Data.EntityClasses;
|
|
using System.Diagnostics;
|
|
using System.Reflection;
|
|
using System.Text;
|
|
//using Outlook = Microsoft.Office.Interop.Outlook;
|
|
|
|
namespace Gremlin_BlazorServer.Services
|
|
{
|
|
internal static class FileService
|
|
{
|
|
private static readonly Encoding defaultEncodingIfNoBom = Encoding.UTF8;
|
|
private static readonly string currentDirectory = Directory.GetCurrentDirectory();
|
|
|
|
public static string ReadResource(string name)
|
|
{
|
|
Assembly assembly = Assembly.GetExecutingAssembly();
|
|
|
|
// Format: "{Namespace}.{Folder}.{filename}.{Extension}"
|
|
string resourcePath = name.StartsWith(nameof(Gremlin_BlazorServer)) ? name : assembly.GetManifestResourceNames().Single(str => str.EndsWith(name));
|
|
|
|
using Stream? stream = assembly.GetManifestResourceStream(resourcePath);
|
|
if (stream == null) { return ""; }
|
|
|
|
using StreamReader reader = new(stream);
|
|
return reader.ReadToEnd();
|
|
}
|
|
|
|
// public static string GetFilepathFromUser(string filter = "Delimited Data File|*.csv; *.txt; *.tsv")
|
|
// {
|
|
// string _filepath = "";
|
|
// OpenFileDialog dlg = new()
|
|
// {
|
|
// Multiselect = false,
|
|
// Filter = filter
|
|
// };
|
|
|
|
// if (dlg.ShowDialog() == true)
|
|
// {
|
|
// //Auswahl dlg.filenames an passende Objekte übergeben
|
|
// _filepath = dlg.FileName;
|
|
// }
|
|
// return _filepath;
|
|
// }
|
|
|
|
// internal static void OpenFile(string path, string file, string type)
|
|
// {
|
|
// try
|
|
// {
|
|
// _ = Process.Start(path == "" ? $"{file}.{type}" : $"{path}{Path.DirectorySeparatorChar}{file}.{type}");
|
|
// }
|
|
// catch (Exception ex)
|
|
// {
|
|
// Debug.WriteLine(ex);
|
|
// }
|
|
// }
|
|
|
|
public static Encoding GetEncoding(string fileName)
|
|
{
|
|
Stream fileStream = File.OpenRead(fileName);
|
|
using StreamReader reader = new(fileStream, defaultEncodingIfNoBom, true);
|
|
_ = reader.Peek();
|
|
Encoding encoding = reader.CurrentEncoding;
|
|
return encoding;
|
|
}
|
|
|
|
public static void WriteClipboardToTsv(string clipboard, Quote quote)
|
|
{
|
|
string datei = $"{quote.Path}{Path.DirectorySeparatorChar}{quote.QuotationNumber}.tsv";
|
|
FileInfo fileInfo = new(datei);
|
|
|
|
if (fileInfo.Directory == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!fileInfo.Directory.Exists)
|
|
{
|
|
try { _ = Directory.CreateDirectory(fileInfo.DirectoryName ?? ""); }
|
|
catch (Exception ex) { Debug.WriteLine(ex); }
|
|
}
|
|
|
|
using StreamWriter writer = new(datei, false, Encoding.UTF8);
|
|
{
|
|
writer.WriteLine(clipboard);
|
|
}
|
|
}
|
|
|
|
public static void WriteTexFile(Quote quote)
|
|
{
|
|
string datei = $"{quote.Path}{Path.DirectorySeparatorChar}{quote.QuotationNumber}.tex";
|
|
|
|
FileInfo fileInfo = new(datei);
|
|
|
|
if (fileInfo.Directory == null)
|
|
{
|
|
return;
|
|
}
|
|
|
|
if (!fileInfo.Directory.Exists) { _ = Directory.CreateDirectory(fileInfo.DirectoryName ?? ""); }
|
|
|
|
using StreamWriter writer = new(datei, false, Encoding.UTF8);
|
|
{
|
|
writer.WriteLine(quote.Tex);
|
|
}
|
|
}
|
|
|
|
//public static void SendQuoteViaMail(QuoteVM quoteVM)
|
|
//{
|
|
// Outlook.Application application = new();
|
|
|
|
// Outlook.MailItem mail = application.CreateItem(Outlook.OlItemType.olMailItem) as Outlook.MailItem;
|
|
|
|
// mail.Subject = quoteVM.QuoteNumber;
|
|
// Outlook.AddressEntry currentUser = application.Session.CurrentUser.AddressEntry;
|
|
|
|
// if (currentUser.Type == "EX")
|
|
// {
|
|
// Outlook.ExchangeUser manager = currentUser.GetExchangeUser().GetExchangeUserManager();
|
|
// // Add recipient using display name, alias, or smtp address
|
|
// mail.Recipients.Add(manager.PrimarySmtpAddress);
|
|
// mail.Recipients.ResolveAll();
|
|
// mail.Attachments.Add($"{quoteVM.QuoteNumber}.pdf", Outlook.OlAttachmentType.olByValue, Type.Missing, Type.Missing);
|
|
// mail.Send();
|
|
// }
|
|
//}
|
|
}
|
|
} |