标签: streamreader

File.ReadLines 相对于应用程序可执行文件的路径?

我正在读取和写入一些文本文件,并且当前正在使用设定路径。如何更改该设置路径以使用可执行文件所在的同一文件夹?

例如:

File.ReadLines(@"D:\Users\MyUserName\Desktop\MyApplication\names.txt").Skip(1).ToList();
Run Code Online (Sandbox Code Playgroud)

和...

File.WriteAllLines(@"D:\Users\MyUserName\Desktop\MyApplication\names.txt", lines);
Run Code Online (Sandbox Code Playgroud)

和...

using (StreamWriter writer = new StreamWriter(@"D:\Users\MyUserName\Desktop\MyApplication\names.txt", true))
{
    writer.WriteLine(myText);
}
Run Code Online (Sandbox Code Playgroud)

在 IDE (Visual Studio) 中测试时它也需要工作。

c# file streamwriter streamreader filepath

0
推荐指数
1
解决办法
2521
查看次数

C# 找不到与程序位于同一目录中的文件

当尝试打开与 位于同一目录中的文件上的 StreamReader 时,我收到 System.IO.FileNotFoundException Program.cs

try
{
    using (StreamReader sr = new StreamReader("file.txt")) { ... }
}
Run Code Online (Sandbox Code Playgroud)

有问题的单词文件与程序源代码位于同一目录中。我正在使用 Visual Studio 2019,这是一个简单的命令行项目。

那么为什么 C# 找不到这个文件,尽管它位于同一目录中呢?

Visual Studio 不会显示整个堆栈跟踪,而只会显示一行错误,这并没有帮助。

c# streamreader

0
推荐指数
1
解决办法
1376
查看次数

C# 中 StreamReader 何时变为 null?

我看到了这个链接:

https://learn.microsoft.com/en-us/dotnet/api/system.idisposable?view=net-6.0

代码部分是:

if (!File.Exists(filename))
    throw new FileNotFoundException("The file does not exist.");

this.filename = filename;
string txt = String.Empty;
StreamReader sr = null;
try
{
    sr = new StreamReader(filename);
    txt = sr.ReadToEnd();
}
finally
{
    if (sr != null) sr.Dispose();
}
Run Code Online (Sandbox Code Playgroud)

StreamReader什么时候变成null?

c# streamreader

0
推荐指数
2
解决办法
169
查看次数

如何从文本文件的开头读取StreamReader

我创建了一个streamReader,但第一个'ReadLine()'读取文件中的第14行,而不是第一行,我不知道为什么会这样.

我的代码是:

StreamReader reader = new StreamReader("argus-BIG.txt");  

//create array to hold data values
string[] branchData = new string[11];

//get 11 lines of branch data file and store in an array
for (int i = 0; i < 11; i++)
{
    branchData[i] = reader.ReadLine();
}            

//new instance of Branch Class
Branch tempBranch = new Branch();

//set branch values from branchData array
tempBranch.setBranchID(branchData[0]);
tempBranch.setBranchNickname(branchData[1]);
tempBranch.setBranchAddressNo(branchData[2]);
tempBranch.setBranchAddressStreet(branchData[3]);
tempBranch.setBranchAddressCity(branchData[4]);
tempBranch.setBranchAddressCountry(branchData[5]);
tempBranch.setBranchAddressPostCode(branchData[6]);            
tempBranch.setNearestBranch1(branchData[7]);
tempBranch.setNearestBranch2(branchData[8]);
tempBranch.setNearestBranch3(branchData[9]);
tempBranch.setNoCategories(Convert.ToInt32(branchData[10]));
Run Code Online (Sandbox Code Playgroud)

我写了一个测试应用程序,它工作,然后复制并粘贴代码(看起来与我相同)回到我的主程序,它工作.谢谢你的帮助

c# streamreader

-1
推荐指数
1
解决办法
1万
查看次数

StreamWriter的Visual Studio bug还是我的误会?

我必须处理一些包含西里尔文本的文件,我在Visual Studio 2012 Ultimate中使用StreamReader/ StreamWriter.

然而,有一些荒谬的问题(或我的误解).以下构造函数工作得很好:

using (StreamReader reader = new StreamReader(someFile, Encoding.Default)){
}
Run Code Online (Sandbox Code Playgroud)

然而,当我尝试以下内容时:

using (StreamWriter writer = new StreamWriter(someOtherfile, Encoding.Default)){
}
Run Code Online (Sandbox Code Playgroud)

我得到一个ivalid构造函数的编译时错误.然而,以下是MSDN的规范:

public StreamWriter(Stream stream, Encoding encoding);
Run Code Online (Sandbox Code Playgroud)

我究竟做错了什么?

c# streamwriter streamreader

-1
推荐指数
1
解决办法
325
查看次数

如何使用StreamReader在特定行之后读取文本文件行

我有一个文本文件,我正在使用StreamReader读取。现在根据我的要求,无论我先读了哪几行,我都不想再读一次,这意味着我不想再次获取该数据。所以我添加了File.ReadLines(FileToCopy).Count();代码来获取首先读取的行。现在,以上代码行返回的任何行,我都想在那之后读取。这是我的代码。

        string FileToCopy = "E:\\vikas\\call.txt";

        if (System.IO.File.Exists(FileToCopy) == true)
        {

            lineCount = File.ReadLines(FileToCopy).Count();

            using (StreamReader reader = new StreamReader(FileToCopy))
            {

            }
         }
Run Code Online (Sandbox Code Playgroud)

我需要在这里指定什么条件。请帮助我。

       while ((line = reader.ReadLine()) != null)
        {

            var nextLines = File.ReadLines(FileToCopy).Skip(lineCount);

        if (line != "")
        {
        }
Run Code Online (Sandbox Code Playgroud)

c# string io streamreader text-files

-1
推荐指数
1
解决办法
8321
查看次数

读取文件并以相反的顺序写入另一个文件,并删除"a"和"the"

我想编写一个程序,使用C#读取一个文件,并以相反的顺序吐出另一个原始文件中包含所有单词的文件,并删除所有单词,如"a"和"the".

using (FileStream stream = File.OpenRead("C:\\file1.txt"))
using (FileStream writeStream = File.OpenWrite("D:\\file2.txt"))
{
    BinaryReader reader = new BinaryReader(stream);
    BinaryWriter writer = new BinaryWriter(writeStream);

    // create a buffer to hold the bytes 
    byte[] buffer = new Byte[1024];
    int bytesRead;

    // while the read method returns bytes
    // keep writing them to the output stream
    while ((bytesRead =
            stream.Read(buffer, 0, 1024)) > 0)
    {
        writeStream.Write(buffer, 0, bytesRead);
    }
}
Run Code Online (Sandbox Code Playgroud)

我已经实现了以前的代码.如何反转和吐出字符"a"和"the".

c# streamreader filereader memory-mapped-files

-2
推荐指数
1
解决办法
825
查看次数

保存到文件损坏 - C#

我尝试使用txt文件作为备份:当激活复选框时,我们在“胜利”txt文件中添加1,当它被停用时,我们在“松散”txt文件中添加1,这是我的问题:\例如,如果禁用该复选框,我运行脚本,在“宽松”中得到 1,在“胜利”中得到 0,所以很正常;但如果我再次运行它(不改变任何东西),我在“宽松”中得到 51,在“胜利”中得到 49,但我想得到 2 和 0..\n我不知道它是否真的很清楚;

\n

这是一段代码:

\n
if (Directory.Exists("C:/PekmiIndustries/BrokenGame"))\n{\n    TextReader VtxtR = new StreamReader("C:/PekmiIndustries/BrokenGame/Victory.txt");\n    TempVReadStr = VtxtR.Read().ToString();\n    VRead = Convert.ToInt32(TempVReadStr);\n    VRead += 1;\n    if (VictoireCheck.Checked == true)\n    {\n        VRead += 1;\n    }\n    VtxtR.Close();\n    TextWriter VtxtW = new StreamWriter("C:/PekmiIndustries/BrokenGame/Victory.txt");\n    VtxtW.Write(VRead.ToString());\n    VtxtW.Close();\n\n    TextReader LtxtR = new StreamReader("C:/PekmiIndustries/BrokenGame/Loose.txt");\n    TempLReadStr = LtxtR.Read().ToString();\n    LRead = Convert.ToInt32(TempLReadStr);\n    LRead += 1;\n    if (VictoireCheck.Checked == false)\n    {\n        LRead += 1;\n    }\n    LtxtR.Close();\n    TextWriter LtxtW = new StreamWriter("C:/PekmiIndustries/BrokenGame/Loose.txt");\n    LtxtW.Write(LRead.ToString());\n    LtxtW.Close();\n\n    MessageBox.Show("Les stats ont correctement \xc3\xa9t\xc3\xa9s …
Run Code Online (Sandbox Code Playgroud)

c# streamwriter streamreader

-2
推荐指数
1
解决办法
94
查看次数

"找不到路径的一部分"C:\ Users\username\abc\abc.exe.config

configFilePath = @"C:\ Users \"+ userName + @"\ abc\abc.exe.config";
if(File.Exists(configFilePath))
{
StreamReader fileReader = new StreamReader(configFilePath);
}

上面的行抛出"找不到路径的一部分"异常.在特定计算机中发生此错误.在所有其他机器中它工作正常.即使在那台机器中,相同的代码也可以使用.机器没有变化.我已经阅读了讨论过这个问题的所有论坛.但无法弄清楚为什么这种情况会在单独的机器中发生,现在也是如此.有权访问文件夹和文件.

c# streamreader filepath

-3
推荐指数
1
解决办法
7432
查看次数

从字符串数组中删除值?

我有150,000行文本文件,它存储为字符串数组,如下所示:

String[] OutputArray = File.ReadAllLines(TB_Complete.Text);
Run Code Online (Sandbox Code Playgroud)

如何从OutputArray中删除指定的行列表?或者更好的是我如何删除前1000行的OutputArray?

*编辑:我需要前1000行读取,解析,输出到另一个文本文件,然后删除.对不起,我没有澄清.

c# arrays streamreader text-files visual-studio

-3
推荐指数
1
解决办法
747
查看次数

如何跳过流阅读器内的一行?

使用streamreader,如果行以'0'开头跳过该行???

string FileToProcess = System.IO.File.ReadAllText(@"up.FolderPath");
using (StreamReader sr = new StreamReader(FileToProcess))
{
    while (!sr.EndOfStream)
    {
        string line = sr.ReadLine();
        if (line.StartsWith("0"))
        {
            line.Skip();
            // ????have to put a number line in here
            // But i just want to skip the line it is on
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

c# asp.net-mvc streamreader asp.net-mvc-3

-4
推荐指数
1
解决办法
3366
查看次数

Streamreader锁定文件

我有一个ac#app(Windows服务),它会触发一个读取目录中文件的计时器事件,并使用文件中的数据发送短信.当事件触发时,它会在处理新文件之前尝试将"已处理"目录中的已处理文件移动到"已完成"目录.我不断得到"另一个进程正在使用的文件"异常,虽然我很确定我处理了使用这些文件的所有内容.如果我停止服务并再次启动它,则会释放文件.有任何想法吗?

//Code that fires the timer    
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Diagnostics;
using System.Linq;
using System.ServiceProcess;
using System.Text;
using System.Timers;

namespace SmsWindowsService
{
    public partial class SmsWindowsService : ServiceBase
    {
        private static System.Timers.Timer aTimer;

        public SmsWindowsService()
        {
            InitializeComponent();

            if (!System.Diagnostics.EventLog.SourceExists("MatterCentreSMSSource"))
            {
                System.Diagnostics.EventLog.CreateEventSource(
                    "MatterCentreSMSSource", "MatterCentreSMSLog");
            }
            elMatterCentreSMS.Source = "MatterCentreSMSSource";
            elMatterCentreSMS.Log = "MatterCentreSMSLog";

        }

        protected override void OnStart(string[] args)
        {
            string logText = string.Empty;

            logText = "MatterCentreSMS Service started successfully on " + DateTime.Now;

            WriteEventLog(logText);

            //Create a timer …
Run Code Online (Sandbox Code Playgroud)

c# locking file streamreader

-5
推荐指数
1
解决办法
1257
查看次数

StreamReader 的 While 循环不起作用

表单在内部停止工作。返回cursor to c=s.Substring...while

StreamReader sr= new StreamReader("Unos.txt");
s = sr.ReadLine();
            c = s.Substring(s.IndexOf(',') + 2, (s.Length - s.IndexOf(',') + 2) - (s.Length - s.IndexOf('-') + 2));
            if ((String.Equals(cbRazred.SelectedItem.ToString(), c.Substring(0, (c.IndexOf('-')))) &&
                String.Equals(cbOdeljenje.SelectedItem.ToString(), odeljenje.ToString()))) lbSpisak.Items.Add(s.Substring(0, s.IndexOf(',')));
            while (s != null)
            {
                s = sr.ReadLine();
                c = s.Substring(s.IndexOf(',') + 2, (s.Length - s.IndexOf(',') + 2) - (s.Length - s.IndexOf('-') + 2));
                if ((String.Equals(cbRazred.SelectedItem.ToString(), c.Substring(0, (c.IndexOf('-')))) &&
                    String.Equals(cbOdeljenje.SelectedItem.ToString(), odeljenje.ToString()))) lbSpisak.Items.Add(s.Substring(0, s.IndexOf(',')));
            }
            sr.Close();
Run Code Online (Sandbox Code Playgroud)

c# streamreader while-loop

-8
推荐指数
1
解决办法
2941
查看次数