如何在Streamwriter在C#中创建文本文件时覆盖 [英] How to overwrite while streamwriter is creating text file in C#

查看:347
本文介绍了如何在Streamwriter在C#中创建文本文件时覆盖的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,



我正在创建一个代码,每次运行时都使用streamwriter创建一个新的文本文件,它将所有UI从文本框放入文本文件。我的问题是...在我的代码中我有一个文本框,设置为将所有输入(ascii)转换为十六进制并将十六进制值写入文本文件的代码,(我已将参数设置为文本框以仅接受alpha-数值,最多只能接受27个字符)。我需要将用户未填充的任何字符(最多27个)替换为0,但需要将(额外的)0写入文本文件而不是转换为十六进制。我只能想要覆盖,但是如何覆盖正在创建的文件。



我已将我的代码的基本部分应用于UI,并将输入转换为十六进制,然后将其写入由以下内容创建的新文本文件:代码。



非常感谢任何建议



Fazila



我尝试过:



Hi All,

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.

I have put the basic part of my code that applies parameters to UI, and converts input to hex, which is then written to a new text file which is created by the code.

Many thanks in advance for any suggestions

Fazila

What I have tried:

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();
        }

推荐答案

hexOutput = hexOutput.PadRight( 27, '0' );
objWriter.WriteLine(hexOutput);


这里有一些你可以适应的想法:
Here's some ideas you could adapt:
using System;
using System.Linq;

namespace YourConverionLibrary
{
    public static class ConversionExtensions
    {
        public static byte[] ToByteAry(this string input, bool filterforAlpha = true)
        {
            if (filterforAlpha)
            {
                input = new string(input.Where(c => char.IsLetterOrDigit(c) || char.IsWhiteSpace(c)).ToArray());
            }

            return input.Select(c => (byte) c).ToArray();
        }

        public static string ToHxFrmBytAry(this byte[] input)
        {
            return BitConverter.ToString(input);
        }
    }
}

样本用法:

var test = "testing 1 2 3 !".ToByteAry().ToHxFrmBytAry();
Console.WriteLine(test);

生成的字符串:



74-65-73-74-69-6E- 67-20-31-20-32-20-33-20



注意决赛! char被排除在外,但最终空格包括在内。

The string generated:

74-65-73-74-69-6E-67-20-31-20-32-20-33-20

note the final ! char is excluded, but the final space is included.


这篇关于如何在Streamwriter在C#中创建文本文件时覆盖的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆