所以我知道一个段落是 char 10 + char 13 我这样做:
streamreader sr = new streamreader();
string s = sr.ReadToEnd();
string s1 = s.Replace((char)10, "*");
string s2 = s1.Replace((char)13, "*");
Run Code Online (Sandbox Code Playgroud)
现在它将段落更改为两个 ** 但如何按 2 个字符拆分?任何人都有其他分割段落的替代方法?
我有一种方法可以从文本文件文件中编辑单词并将其显示在命令提示符上。现在我尝试制作一种方法来编辑文本文件中的单词并将其写入新文件。我还想提一下,我不能使用 File 或 Regex 类,因为我不允许在我的作业中使用它。这是我的 StreamReader 代码:
public void EditorialControl(string fileName, string word, string replacement)
{
List<string> list = new List<string>();
using (StreamReader reader = new StreamReader(directory + fileName))
{
string line;
while ((line = reader.ReadLine()) != null)
{
line = line.Replace(word, replacement);
list.Add(line);
Console.WriteLine(line);
}
reader.Close();
}
}
Run Code Online (Sandbox Code Playgroud)
到目前为止,这是我的 StreamWriter 代码:
public void EditorialResponse(string fileName, string word, string replacement, string saveFileName)
{
using (StreamWriter writer = new StreamWriter(directory + saveFileName, true))
{
{
string input = directory + fileName; …Run Code Online (Sandbox Code Playgroud) 我需要从 XML 文件中删除回车,但我得到的只是一个带有一个框的 XML 文件(\ r)。我是这样写的:
string sourceFileName = textBox1.Text;
StreamReader sr = new StreamReader(sourceFileName);
String line;
String newLine = null;
// Read and display lines from the file until the end of
// the file is reached.
while ((line = sr.ReadLine()) != null)
{
//Regex regex = new Regex(@"(\r\n|\r|\n)+");
newLine = line.Replace("\r\n", String.Empty);
//also tried line.Replace("\n", String.Empty).Replace("\r",String.Empty); , but no use
StreamWriter sw = new StreamWriter(@"C:\Users\manish\Desktop\output.xml");
sw.WriteLine(newLine);
sw.Close();
}
Run Code Online (Sandbox Code Playgroud)
这里可能有什么问题。
编辑:
这是示例:
3C 3F 78 6D 6C 20 76 …Run Code Online (Sandbox Code Playgroud) 我的 sql-query 的结果是我想在标签中显示但不知道如何显示的单个值。我有一个创建数据集的解决方案,将查询结果放入该数据集并按行(0),列(0)获取它,但我想有一种更聪明的方法。
Private myquery As String
Sub New(ByVal query As String)
myquery = query
End Sub
Public Sub query_Send()
Dim myconn As New SqlConnection("Data Source=DB;Integrated Security=TRUE")
Dim mycmd As SqlCommand = New SqlCommand(Me.myQuery, myconn)
Dim myTable As New DataTable
Dim previousConnectionState As ConnectionState = myconn.State
Try
If myconn.State = ConnectionState.Closed Then
myconn.Open()
Dim myDA As New SqlDataAdapter(mycmd)
myDA.Fill(myTable)
tb.text = **...**
End If
If previousConnectionState = ConnectionState.Closed Then
myconn.Close()
End If
End Try
End Sub
Run Code Online (Sandbox Code Playgroud) private void open(object sender, EventArgs e)
{
OpenFileDialog openDialog = new OpenFileDialog();
if (openDialog.ShowDialog() == DialogResult.OK)
{
string[] lines = File.ReadAllLines(openDialog.FileName);
foreach (string line in lines)
{
var text = line.Split(',', '\n');
dataGridView1.Rows.Add(text);
}
}
}
Run Code Online (Sandbox Code Playgroud)
所以在这段代码中我可以读取一个文本文件,但我只能像在1格一样.以下是示例文本文件的外观:
test,1,2,3,4,5,6,
testing,7,8,9,10,11,12,
working,13,14,15,16,17,18,
multiline,19,20,21,22,23,24,
NEXTDATA
see,25,26,36,46,56,66,
program,7,8,9,10,11,12,
macroer,13,14,15,16,17,18,
readextra,19,20,21,22,23,24,
Run Code Online (Sandbox Code Playgroud)
因此,如果我到达名为"NEXTDATA"的行,我希望将数据放入其中dataGridView2.如何检查它是否到达该行,然后它将执行与代码相同的操作dataGridView1?
我也使用ReadLine()或ReadAllLines()?
如何打开 FileStream,读取文件内容,然后在不重新打开文件的情况下写入(追加)到文件?
在这种情况下 StreamReader/StreamWriter 的一个问题是它们承担了底层流的所有权。由于目标是 .NET 4,因此不能使用“leaveOpen”构造函数重载。(我不在乎是否使用了 StreamReader/StreamWriter - 但它们确实提供了 ReadLine 和 WriteLine 操作。)
总之,与问题相关的问题代码示例以及应用程序将如何访问和管理 FileStream 的生命周期(即打开一次):
var fs = File.Open(..);
using (var reader = new StreamReader(fs)) {
// Do all reading here, then ditch the reader
} // .. but StreamReader will Close the FileStream
SeekToEnd(fs);
using (var writer = new StreamWriter(fs)) {
// Do all writing here, then ditch the writer
// .. ideal, but FileStream already Closed
}
// …Run Code Online (Sandbox Code Playgroud) 我正在使用C#中的StreamReader.我收到了错误
"无法找到类型或命名空间名称"StreamReader""
我不知道我做错了什么.
using System.IO;
using System;
class Perfect
{
static void Main()
{
string filename = @"marks.cvs";
StreamReader sr = new StreamReader(filename);
string line = sr.ReadLine();
Console.WriteLine(line);
sr.Close();
}
}
Run Code Online (Sandbox Code Playgroud) 我正在使用 AWS Unity (v3.3.50.0):从https://aws.amazon.com/mobile/sdk/下载的 S3 SDK (AWSSDK.S3.3.3.5.4.unitypackage) 。我的 Unity 版本是 5.5.1。
我想下载一个放在 S3 存储桶上的图像,存储桶已配置并且可以下载。我将字符串视为响应数据。
但我无法在 S3 示例 GetObject() 函数中将返回的 StreamReader 转换为 UnityEngine.UI.Image.sprite 或 UnityEngine.UI.RawImage.texture。
private void GetObject()
{
ResultText.text = string.Format("fetching {0} from bucket {1}", SampleFileName, S3BucketName);
Client.GetObjectAsync(S3BucketName, SampleFileName, (responseObj) =>
{
string data = null;
var response = responseObj.Response;
if (response.ResponseStream != null)
{
using (StreamReader reader = new StreamReader(response.ResponseStream))
{
data = reader.ReadToEnd();
}
ResultText.text += "\n";
ResultText.text += data;
}
Debug.Log("GetObject: " + data);
}); …Run Code Online (Sandbox Code Playgroud) 我尝试制作一个脚本来逐行读取 TXT 文件,并根据里面的内容更改标签。有没有办法检查正在读取哪一行?
对于我的 Visual Basic 期末考试,我的程序需要将文本文件中的数据读取到两个不同的数组中,每个数组都是一维的。以下是我这样做的代码:
Option Explicit On
Option Infer Off
Option Strict On
Public Class frmMain
'Constant for filename and a dirty flag variable
Const INVENTORY_FILENAME As String = "inventory.txt"
Dim noFile As Boolean = False
Private Sub frmMain_Load(sender As Object, e As EventArgs) Handles MyBase.Load
'Populates DVD listbox with text file data upon load
'Variable for reading the file
Dim myFile As IO.StreamReader
'Declaring arrays for DVD names and prices
Dim arrayDVD() As String
Dim arrayPrice() As Double
'Variables …Run Code Online (Sandbox Code Playgroud) streamreader ×10
c# ×8
.net ×3
streamwriter ×2
text-files ×2
vb.net ×2
.net-4.0 ×1
amazon-s3 ×1
arrays ×1
aws-sdk ×1
dataset ×1
file-io ×1
filestream ×1