我正在读取和写入一些文本文件,并且当前正在使用设定路径。如何更改该设置路径以使用可执行文件所在的同一文件夹?
例如:
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) 中测试时它也需要工作。
当尝试打开与 位于同一目录中的文件上的 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 不会显示整个堆栈跟踪,而只会显示一行错误,这并没有帮助。
我看到了这个链接:
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?
我创建了一个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)
我写了一个测试应用程序,它工作,然后复制并粘贴代码(看起来与我相同)回到我的主程序,它工作.谢谢你的帮助
我必须处理一些包含西里尔文本的文件,我在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)
我究竟做错了什么?
我有一个文本文件,我正在使用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#读取一个文件,并以相反的顺序吐出另一个原始文件中包含所有单词的文件,并删除所有单词,如"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".
我尝试使用txt文件作为备份:当激活复选框时,我们在“胜利”txt文件中添加1,当它被停用时,我们在“松散”txt文件中添加1,这是我的问题:\例如,如果禁用该复选框,我运行脚本,在“宽松”中得到 1,在“胜利”中得到 0,所以很正常;但如果我再次运行它(不改变任何东西),我在“宽松”中得到 51,在“胜利”中得到 49,但我想得到 2 和 0..\n我不知道它是否真的很清楚;
\n这是一段代码:
\nif (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) configFilePath = @"C:\ Users \"+ userName + @"\ abc\abc.exe.config";
if(File.Exists(configFilePath))
{
StreamReader fileReader = new StreamReader(configFilePath);
}
上面的行抛出"找不到路径的一部分"异常.在特定计算机中发生此错误.在所有其他机器中它工作正常.即使在那台机器中,相同的代码也可以使用.机器没有变化.我已经阅读了讨论过这个问题的所有论坛.但无法弄清楚为什么这种情况会在单独的机器中发生,现在也是如此.有权访问文件夹和文件.
我有150,000行文本文件,它存储为字符串数组,如下所示:
String[] OutputArray = File.ReadAllLines(TB_Complete.Text);
Run Code Online (Sandbox Code Playgroud)
如何从OutputArray中删除指定的行列表?或者更好的是我如何删除前1000行的OutputArray?
*编辑:我需要前1000行读取,解析,输出到另一个文本文件,然后删除.对不起,我没有澄清.
使用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) 我有一个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) 表单在内部停止工作。返回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# ×13
streamreader ×13
streamwriter ×3
file ×2
filepath ×2
text-files ×2
arrays ×1
asp.net-mvc ×1
filereader ×1
io ×1
locking ×1
string ×1
while-loop ×1