KOP-PIbd-32-Katysheva-N-E/ComponentsLibrary/TestComponent.cs

63 lines
1.2 KiB
C#
Raw Normal View History

2024-09-30 22:43:40 +04:00
using ComponentsView;
using System.ComponentModel;
using static System.Net.Mime.MediaTypeNames;
namespace ComponentsLibrary
{
public partial class TestComponent : Component
{
private string _fileName;
//DocumentEntry doc = new DocumentEntry("C:\\Users\\Natalia\\Desktop\\5semestr\\KOP\\KOP-PIbd-32-Katysheva-N-E", "doc.docx", text);
public string FileName
{
set
{
if (string.IsNullOrEmpty(value))
{
return;
}
if (!value.EndsWith(".txt"))
{
throw new ArgumentException("No txt file");
}
_fileName = value;
}
}
public TestComponent()
{
InitializeComponent();
_fileName = string.Empty;
}
public TestComponent(IContainer container)
{
container.Add(this);
InitializeComponent();
_fileName = string.Empty;
}
public bool SaveToFile(string[] texts)
{
CheckFileExsists();
using var writer = new StreamWriter(_fileName, true);
foreach (var text in texts)
{
writer.WriteLine(text);
}
writer.Flush();
return true;
}
private void CheckFileExsists()
{
if (string.IsNullOrEmpty(_fileName))
{
throw new ArgumentNullException(_fileName);
}
if (!File.Exists(_fileName))
{
File.Create(_fileName).Dispose();
}
}
}
}