Tag: file io

如何将文本附加到多个文件

我不是程序员,但我是研究员,我需要修改一些文件。 我有许多带* .mol扩展名的文本文件位于c:\ abc \目录中。 我需要将包含以下文本“M END”的行附加到此列表中的每个文件。 我尝试在C#中使用但没有任何结果: using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.IO; namespace ConsoleApplication3 { class Program { static void Main(string[] args) { StreamWriter sw = new StreamWriter(“c:\\abc\\*.mol”, true); sw.WriteLine(“M END”); sw.Close(); } } } 请建议解决方案。 谢谢!

创建文件而不打开/锁定它?

有没有人知道一种方法(相当简单)创建一个文件而不实际打开/锁定它? 在File类中,文件创建方法始终返回FileStream。 我想要做的是创建一个文件,重命名它(使用File.Move),然后使用它。 现在我必须: 创造它 关 改名 重新开放使用

获取最新的N个文件并删除其余文件

我正在尝试编写一个从文件夹中获取文件的方法,按创建时间对其进行排序,获取前五个最新文件并删除其余文件。 任何帮助将不胜感激,我的代码如下: DirectoryInfo Dir = new DirectoryInfo(DirectoryPath); FileInfo[] FileList = Dir.GetFiles(“*.*”, SearchOption.AllDirectories); var x = FileList.OrderByDescending(file => file .CreationTime).Take(5); 如何修改此代码以删除所有其他文件?

为什么我的Stream不可读?

这是我的问题的续集,关于修剪日志文件中的脂肪。 我有这个代码: 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 […]

C#File.ReadAllLines没有中断换行

我有一个我正在构建的应用程序,需要修改配置文件。 我的问题是我无法逐行读取文件。 我一直把整个文件作为单个字符串进行geeting。 string ConfigTemplate = AEBuildsSPFolderName + “\\Template_BuildReleaseScript.Config”; string[] fileSourceLines = File.ReadAllLines(ConfigTemplate, Encoding.Default); //Returns the entire file contents into the first array element. using (StreamReader reader = new StreamReader(ConfigTemplate)) { string line; while ((line = reader.ReadLine()) != null) //Returns the entire file contents into the first line read. 我知道我做错了什么? 谢谢, 大卫

将文件保存在项目内的指定文件夹中

我正在创建一个Xml文件,我想将它保存在我可以在解决方案资源管理器中访问它的解决方案中的项目内的指定文件夹中。 如何指定路径以便创建文件夹并将xml文件保存在其中? 因为它目前在我的项目的根目录中创建文件,我无法在解决方案资源管理器中查看它。 XmlSerializer serializer = new XmlSerializer(typeof(BoxSet)); TextWriter textWriter = new StreamWriter(“../../Box.xml”); 对不起,还是个新手…我错过了什么。

如何检查文件是否在c#中使用?

如何检查我正在使用的excel文件(操作其数据,删除或覆盖它)是否正被另一个程序使用? 以及如何从同一版本中释放它? 请指导我使用C#。

如何将一个巨大的文件分成单词?

如何从文本文件中读取一个非常长的字符串,然后处理它(拆分成单词)? 我尝试了StreamReader.ReadLine()方法,但是我得到了OutOfMemoryexception。 显然,我的线条非常长。 这是我读取文件的代码: using (var streamReader = File.OpenText(_filePath)) { int lineNumber = 1; string currentString = String.Empty; while ((currentString = streamReader.ReadLine()) != null) { ProcessString(currentString, lineNumber); Console.WriteLine(“Line {0}”, lineNumber); lineNumber++; } } 以及将行分为单词的代码: var wordPattern = @”\w+”; var matchCollection = Regex.Matches(text, wordPattern); var words = (from Match word in matchCollection select word.Value.ToLowerInvariant()).ToList();

读取文件的最后30,000行

如果有一个csv文件,其数据会不时增加。 现在我需要做的是阅读最后30,000行。 代码: string[] lines = File.ReadAllLines(Filename).Where(r => r.ToString() != “”).ToArray(); int count = lines.Count(); int loopCount = count > 30000 ? count – 30000 : 0; for (int i = loopCount; i < lines.Count(); i++) { string[] columns = lines[i].Split(','); orderList.Add(columns[2]); } 它工作正常,但问题是 File.ReadAllLines(Filename) 阅读导致性能不足的完整文件。 我想要它只读取最后30,000行迭代整个文件。 PS:我正在使用.Net 3.5。 Files.ReadLines()在.Net 3.5中不存在

Process.Start()的替代方案

我刚刚完成编码文档存储解决方案,我遇到了以下问题。 在UI中,用户可以按下按钮打开文件: try { Process.Start(file); } catch (Exception ex) { //Error handling code } 我的问题是,如果用户没有与文件类型相关联的应用程序,则会抛出一个componentmodelexception,并显示一条消息。 我宁愿做的是在那种情况下弹出“打开方式”对话框,是否有方法调用我不见了?