Как перезаписать в то время как streamwriter создает текстовый файл в C#
Всем Привет,
I am creating a code which uses streamwriter to create a new text file each time it is run, it puts all UI from textboxes into a text file. my issue is... in my code i have a textbox which is set to a code that converts all input (ascii) to hex and writes the hex values to text file, (I have set parameters to the textbox to accept only alpha-numeric values, and to only accept upto 27 characters). I need for any characters not filled by the user (up to 27) to be replaced by a 0, but need the (extra) 0 to be written to the text file and not turned to hex. I can only think to overwrite, but how to overwrite a file which is in the process of being created.
Я поместил основную часть своего кода, которая применяет параметры к пользовательскому интерфейсу и преобразует входные данные в шестнадцатеричный код, который затем записывается в новый текстовый файл, созданный кодом.
Заранее большое спасибо за любые предложения
Фазила
Что я уже пробовал:
using System; using System.Collections.Generic; using System.ComponentModel; using System.Data; using System.Drawing; using System.Linq; using System.Text; using System.Threading.Tasks; using System.Windows.Forms; namespace Ascii_to_hex { public partial class Form1 : Form { public Form1() { InitializeComponent(); } private void textBox2_TextChanged(object sender, EventArgs e) { } private void textBox2_KeyPress(object sender, KeyPressEventArgs e) { if (!char.IsControl(e.KeyChar) && !char.IsLetterOrDigit(e.KeyChar)) { e.Handled = true; } } private void button2_Click(object sender, EventArgs e) { System.IO.StreamWriter objWriter; string name = label1.Text; objWriter = new System.IO.StreamWriter(name + ".inp"); string str = textBox2.Text; char[] charValues = str.ToCharArray(); string hexOutput = ""; foreach (char _eachChar in charValues) { int value = Convert.ToInt32(_eachChar); hexOutput += String.Format("{0:X}", value); } objWriter.WriteLine(hexOutput); objWriter.Close(); }