我如何获得a的长度StreamReader,因为我知道将不再写入任何内容。我以为也许可以将所有数据传递给a MemoryStream,该方法有一个称为的方法Length,但是我对如何将byte []附加到a感到困惑MemoryStream。
private void Cmd(string command, string parameter, object stream)
{
StreamWriter writer = (StreamWriter)stream;
StreamWriter input;
StreamReader output;
Process process = new Process();
try
{
process.StartInfo.UseShellExecute = false;
process.StartInfo.CreateNoWindow = true;
process.StartInfo.RedirectStandardOutput = true;
process.StartInfo.RedirectStandardInput = true;
process.StartInfo.FileName = "cmd";
process.Start();
input = process.StandardInput;
output = process.StandardOutput;
input.WriteLine(command + " " + parameter);
input.WriteLine("exit");
using (MemoryStream ms = new MemoryStream())
{
int length = 1024;
char[] charbuffer = new char[length];
byte[] …Run Code Online (Sandbox Code Playgroud) Greatings!我需要反序列化序列化为json的不同对象的文件.这是结果文件:
{
"Number": 1,
"Description": "Run version with strategy data",
"Context": "NA"
}[
{
"N": 0.0,
"T": 2.0,
"Adc": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
],
"SpltFr": 2.0,
"Acc": 1.0,
"DAcc": 0.0,
"Acc2": 1.0,
"OscFr": 0.5,
"Fltr": 0,
"CmpEr": false,
"ErrPck": 0,
"IndxDiff": 0,
"Pos": 0,
"FastAcc": [],
"GIndx": 0,
"Indx": 0,
"PcTime": "0001-01-01T00:00:00"
},
{
"N": 1.0,
"T": 2.0,
"Adc": [
0.0,
0.0,
0.0,
0.0,
0.0,
0.0,
0.0
],
"SpltFr": 2.2999999523162842,
"Acc": 1.0,
"DAcc": 0.0,
"Acc2": 1.0, …Run Code Online (Sandbox Code Playgroud) 这是我在这里有关从日志文件中删除胖文件的问题的后遗症。
我有以下代码:
private readonly FileStream _fileStream;
private readonly StreamWriter _streamWriter;
. . .
const int MAX_LINES_DESIRED = 1000;
string uriPath = GetExecutionFolder() + "\\Application.log";
string localPath = new Uri(uriPath).LocalPath;
if (!File.Exists(localPath))
{
File.Create(localPath);
}
_fileStream = File.OpenWrite(localPath);
// First, remove the earliest lines from the file if it's grown too much
StreamReader reader = new StreamReader(_fileStream);
. . .
Run Code Online (Sandbox Code Playgroud)
在显示的最后一行失败:
System.ArgumentException was unhandled
_HResult=-2147024809
_message=Stream was not readable.
Run Code Online (Sandbox Code Playgroud)
为什么不可读?我以为可能是因为文件为空,但是我向其中添加了一行,并且仍然得到相同的err msg。
我有一个StreamWriter对象写入日志文件,它初始化如下:
StreamWriter streamWriter = File.AppendText(GetLogFilePath());
streamWriter.AutoFlush = true;
Run Code Online (Sandbox Code Playgroud)
稍后在我的代码中,我需要关闭日志文件,然后读取部分已注销的内容.我有以下代码:
streamWriter.Close();
streamWriter = null;
StreamReader logFileStream = File.OpenText(GetLogFilePath());
Run Code Online (Sandbox Code Playgroud)
抛出异常: 进程无法访问该文件,因为它正由另一个进程使用.
据推测,问题是当我关闭StreamWriter时,日志文件未正确关闭.
有没有更好的方法来关闭StreamWriter以确保它已打开的文件正确关闭?
更新:我已经解决了我的问题.它是由一个无关的问题(一个单独的应用程序正在访问我试图访问的文件)引起的.我接受Dmitry Martovoi的回答,因为我认为这对将来有类似问题的其他人最有用; 但重要的一点是,解决方案确实需要在每次写入日志时打开日志文件,这可能会导致不必要的开销.
看到解决方案的评论 - 文件在错误的地方
我到处寻找答案,但我找不到答案.这对我来说真的很令人沮丧,因为我从来没有从使用任何其他编程语言的文件中读取这么多麻烦.
我正在尝试从文本文件中提取用户名和密码,以获得基本的即时消息程序.我不打算发布所有代码 - 它太长了,而且很可能不相关,因为文本文件是在程序的最开始读取的.
这是我试图读取的文本文件("users.ul")的内容:
admin.password
billy.bob
sally.sal
Run Code Online (Sandbox Code Playgroud)
以下是从文本文件中读取的代码:
users = new Dictionary<string, User>();
System.Console.WriteLine("users.ul exists: " + File.Exists("users.ul"));
// Check the status of users.ul. If it exists, fill the user dictionary with its data.
if (File.Exists("users.ul"))
{
// Usernames are listed first in users.ul, and are followed by a period and then the password associated with that username.
StreamReader reader = new StreamReader("users.ul");
string line;
int count = 0;
while ((line = reader.ReadLine()) != null)
{ …Run Code Online (Sandbox Code Playgroud) 我正在尝试搜索他们居住的城镇中存储在文本文件中的客户帐户.有三个客户帐户都有不同的城镇.我认为该程序找到了正确的客户,因为它确实返回数据写入屏幕但不是全部.这些图像更好地解释了它.试图嵌入他们,但没有积分
文本文件内容:

当搜索住在利物浦的顾客时(尽管图像没有显示,但光标开始在DOB下面闪烁5行)

寻找贝尔法斯特客户.请注意,它会选择coreect DOB,但错误的客户编号和Surname.(我把评论中的链接放在一起不会让我发布超过2个链接)
下面是方法的代码::
static void FindTown(CustomerStruct[] CustomerDeats)
{
string City;
bool CityMatch = false;
Console.Clear();
begin:
try
{
Console.WriteLine("Please enter the customers Town ");
City = Console.ReadLine();
}
catch
{
Console.WriteLine("Failed. Please try again.");
goto begin;
}
var pathToTown = @"..\..\..\Files\Customer.txt";
using (StreamReader sr = new StreamReader(pathToTown))
while (!CityMatch)
{
{
RecCount = 0;
CustomerDeats[RecCount].Town = sr.ReadLine();
if (City == CustomerDeats[RecCount].Town)
{
Console.WriteLine("\n\n");
CityMatch = true;
CustomerDeats[RecCount].CustomerNo = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].CustomerNo);
CustomerDeats[RecCount].Surname = sr.ReadLine();
Console.WriteLine(CustomerDeats[RecCount].Surname);
CustomerDeats[RecCount].Forename = sr.ReadLine(); …Run Code Online (Sandbox Code Playgroud) 我在 Visual Studio 中有一个 C# 项目,它下载并解析包含韩语、中文和其他 unicode 字符的 XML 文件。例如,对于名为Taeyang 的韩国艺术家,它会生成这样的 XML:
<name>??</name>
Run Code Online (Sandbox Code Playgroud)
但它返回
<name>??</name>
Run Code Online (Sandbox Code Playgroud)
我试过了,StreamReader Encoding.Default但结果是
<name>태양</name>
Run Code Online (Sandbox Code Playgroud)
编码:
string address = String.Format("http://musicbrainz.org/ws/2/artist/{0}?inc=url-rels", mbids[ord]);
HttpWebRequest newRequest = WebRequest.Create(address) as HttpWebRequest;
newRequest.Headers["If-None-Match"] = etagProf;
newRequest.Headers[HttpRequestHeader.AcceptEncoding] = "gzip";
var response = newRequest.GetResponse();
// Reader
Stream stream = response.GetResponseStream();
StreamReader reader = new StreamReader(stream, Encoding.UTF-8);
string data = reader.ReadToEnd();
Run Code Online (Sandbox Code Playgroud)
和 xml 源代码:
<?xml version="1.0" encoding="UTF-8"?>
<metadata xmlns="http://musicbrainz.org/ns/mmd-2.0#">
<artist type="Person" id="d84e5667-3cbe-4556-b551-9d7e4be95d71">
<name>??</name>
<sort-name>Taeyang</sort-name><gender>Male</gender>
<country>KR</country>
...........
</artist>
</metadata>
Run Code Online (Sandbox Code Playgroud)
我很困惑,为什么会这样?任何想法伙计?
我刚开始学习C#,看起来在写输出文件或读取输入文件时,需要提供绝对路径,如下所示:
string[] words = { "Hello", "World", "to", "a", "file", "test" };
using (StreamWriter sw = new StreamWriter(@"C:\Users\jackf_000\Projects\C#\First\First\output.txt"))
{
foreach (string word in words)
{
sw.WriteLine(word);
}
sw.Close();
}
Run Code Online (Sandbox Code Playgroud)
MSDN 的示例使您在实例化 StreamWriter 时看起来需要提供绝对目录:
https://msdn.microsoft.com/en-us/library/8bh11f1k.aspx
我用 C++ 和 Python 编写过,在访问这些语言的文件时不需要提供绝对目录,只需提供可执行文件/脚本的路径。每次要读取或写入文件时都必须指定绝对路径似乎很不方便。
有没有什么快速的方法可以抓取当前目录并将其转换为字符串,并将其与输出文件字符串名称结合起来?使用绝对目录是一种很好的风格还是更喜欢,如果可能的话,快速将它与“当前目录”字符串结合起来?
谢谢。
我尝试读取 Oracle BLOB 字段并显示 RichTextBox 中的内容。我在谷歌上找到的例子几乎相同,但我仍然无法让它工作。我知道 BLOB 字段包含序列化数据。这就是我到目前为止所拥有的:(连接阅读器工作正常)
private void button1_Click_1(object sender, EventArgs e)
{
//testen of een blob is uit te lezen
OracleCommand cmd = new OracleCommand();
cmd.Connection = OraConnection.conn;
cmd.CommandText = "select id, blobfield from test_table where id = '20ED7EDB-406A-43E8-945B-5E63DFCBA7FF'";
cmd.CommandType = CommandType.Text;
OracleDataReader dr = cmd.ExecuteReader();
dr.Read();
OracleBlob BLOB1 = dr.GetOracleBlob(1);
Byte[] Buffer = (Byte[])(dr.GetOracleBlob(1)).Value;
string lookupValue = System.Text.ASCIIEncoding.ASCII.GetString(Buffer);
richTextBox1.Text += lookupValue; //shows: DQStream
richTextBox1.Text += "";
richTextBox1.Text += "1";
richTextBox1.Text += dr.GetOracleBlob(1).Value; //shows: System.Byte[]
richTextBox1.Text += …Run Code Online (Sandbox Code Playgroud) 我正在尝试使用 AES 解密 C# 中的字符串:
public static string AesDecrypt(byte[] cipherText, byte[] Key, byte[] IV)
{
string plaintext = null;
// Create an Aes object with the specified key and IV
using Aes aesAlg = Aes.Create();
aesAlg.Padding = PaddingMode.Zeros;
aesAlg.Key = Key;
aesAlg.IV = IV;
// Create a decryptor to perform the stream transform
ICryptoTransform decryptor = aesAlg.CreateDecryptor(aesAlg.Key, aesAlg.IV);
// Create the streams used for decryption
using MemoryStream msDecrypt = new MemoryStream(cipherText);
using CryptoStream csDecrypt = new CryptoStream(msDecrypt, decryptor, CryptoStreamMode.Read);
using …Run Code Online (Sandbox Code Playgroud) c# ×10
streamreader ×10
streamwriter ×2
blob ×1
file-io ×1
filestream ×1
json ×1
json.net ×1
memorystream ×1
oracle ×1
path ×1
unicode ×1
uri ×1