Tag: file io

FileSystemWatcher – 只有一次触发更改事件?

我正在使用以下代码来监听我从服务器下载并打开的文件的更改事件。 但是更改事件仅在第一次保存文件时被触发,然后在后续保存时文件观察者不会触发更改事件? 有谁能看到最新情况? private FileSystemWatcher StartWatchingFile() { fw = new FileSystemWatcher(); fw.Path = this.directoryLocation; fw.Filter = this.Filename; fw.NotifyFilter = NotifyFilters.LastAccess | NotifyFilters.LastWrite; // Add event handler fw.Changed += new FileSystemEventHandler(fw_Changed); // Open file System.Diagnostics.Process.Start(this.CreateAbsoluteFilePath(this.Filename)); // Begin watching. fw.EnableRaisingEvents = true; return fw; } //************************ void fw_Changed(object sender, FileSystemEventArgs e) { MessageBox.Show(“File: ” + e.FullPath + […]

如果文件夹包含空格,如何处理文件路径中的空格?

public static void launchProcess(string processName, string arguments, out string output) { Process p = new Process { StartInfo = { UseShellExecute = false, RedirectStandardOutput = true, FileName = processName, Arguments = arguments } }; p.Start(); output = p.StandardOutput.ReadToEnd(); p.WaitForExit(); } 如果我的参数包含文件名,例如: D:\Visual Studio Projects\ProjectOnTFS\ProjectOnTFS 然后我得到错误:

检测C#中的文件读取

我正在使用FileSystemWatcher检查文件何时被修改或删除,但我想知道是否有任何方法可以检查其他应用程序何时读取文件。 示例:我的硬盘驱动器上有文件C:\ test.txt,正在使用FileSystemWatcher查看它。 另一个程序(不在我的控制之下)去读取该文件; 我想抓住该事件,如果可能的话,检查正在读取文件的程序,然后相应地修改文件的内容。

获取当前文件长度/ FileInfo.Length缓存和陈旧信息

我正在跟踪文件的文件夹及其文件长度,至少有一个文件仍在写入。 我必须不断更新每个文件长度的记录,我将其用于其他目的。 Update方法每15秒调用一次,如果文件长度与上一次更新中确定的长度不同,则更新文件的属性。 update方法如下所示: var directoryInfo = new DirectoryInfo(archiveFolder); var archiveFiles = directoryInfo.GetFiles() .OrderByDescending(f=>f.CreationTimeUtc); foreach (FileInfo fi in archiveFiles) { //check if file existed in previous update already var origFileProps = cachedFiles.GetFileByName(fi.FullName); if (origFileProps != null && fi.Length == origFileProps.EndOffset) { //file length is unchanged } else { //Update the properties of this file //set […]

如何在没有CRLF的情况下在C#中编写WriteAllLines

我正在使用C#并尝试输出几行到ASCII文件。 我遇到的问题是我的Linux主机将这些文件视为: ASCII text, with CRLF line terminators 我需要这个文件只是: ASCII text CRLF引起了一些问题,我希望在C#中有一种方法可以创建以我想要的方式格式化的文件。 我基本上使用这段代码: string[] lines = { “Line1”, “Line2” }; File.WriteAllLines(myOutputFile, lines, System.Text.Encoding.UTF8); 有没有简单的方法来创建没有CRLF行终止符的文件? 我可以在Linux方面处理它,但宁愿从一开始就以适当的格式创建文件。

为什么我的Documents and Settings文件夹出现拒绝访问错误?

我正在编写一个获取所有目录和子目录的程序。 我正在使用以下代码: DriveInfo[] drives = DriveInfo.GetDrives(); foreach (DriveInfo drive in drives) { string[] directories = Directory.GetDirectories(drive.Name, “*”, SearchOption.AllDirectories); } 但我得到一个例外,说明“访问路径’C:\ Documents and Settings \’被拒绝。” 我正在使用Windows 7,我在资源管理器中看不到C:\ Documents and Settings \文件夹。 我启用了“显示隐藏文件和文件夹”,甚至尝试直接键入路径,但它出现以下错误:“C:\ Documents and Settings无法访问。访问被拒绝。” 为什么Directory.GetDirectories()拉出一个似乎不存在的目录?

使用System.IO在C#中复制文件夹

我需要将文件夹C:\ FromFolder复制到C:\ ToFolder 下面的代码将切断我的FromFolder,然后将创建我的ToFolder。 所以我的FromFolder将会消失,所有项目都将在新创建的名为ToFolder的文件夹中 System.IO.Directory.Move(@”C:\FromFolder “, @”C:\ToFolder”); 但我只想将FromFolder中的文件复制到ToFolder。 由于某种原因,没有System.IO.Directory.Copy ??? 如何使用批处理文件完成此操作 – 非常简单 xcopy C:\ FromFolder C:\ ToFolder 问候艾蒂安

极大的单行文件解析

我正在从一个站点下载数据,该站点以非常大的块为我提供数据。 在非常大的块中,我需要单独解析“块”。 这些“块”以“(ClinicalData)”开头,以“(/ ClinicalData)”结尾。 因此,示例字符串看起来像: (ClinicalData)(ID=”1″)(/ClinicalData)(ClinicalData)(ID=”2″)(/ClinicalData)(ClinicalData)(ID=”3″)(/ClinicalData)(ClinicalData)(ID=”4″)(/ClinicalData)(ClinicalData)(ID=”5″)(/ClinicalData) 在“理想”情况下,块意味着是单行数据,但有时会出现错误的换行符。 由于我想解析块中的(ClinicalData)块,我想逐行解析数据。 因此,我取文本文件,将其全部读入StringBuilder,删除新行(以防万一),然后插入我自己的换行符,这样我就可以逐行读取。 StringBuilder dataToWrite = new StringBuilder(File.ReadAllText(filepath), Int32.MaxValue); // Need to clear newline characters just in case they exist. dataToWrite.Replace(“\n”, “”); // set my own newline characters so the data becomes parse-able by line dataToWrite.Replace(“<ClinicalData", "\n<ClinicalData"); // set the data back into a file, which is then used […]

使用StreamReader检查文件是否包含字符串

我有一个字符串是args[0] 这是我的代码到目前为止: static void Main(string[] args) { string latestversion = args[0]; // create reader & open file using (StreamReader sr = new StreamReader(“C:\\Work\\list.txt”)); { while (sr.Peek() >= 0) { //code here } } } 我想检查我的list.txt文件是否包含args[0] 。 如果我有,那么我将创建另一个进程StreamWriter将字符串1写入文件,或者将0写入文件。 我该怎么做?

File.Copy()到具有网络凭据的文件服务器

我正在编写控制台应用程序,它将文件从我的本地磁盘复制到文件服务器。 此文件夹受用户名和密码保护。 File.Copy()方法不起作用。 它给出了权限错误。 我看过这段代码 我试过了,但它不起作用。 首先它是用VB编写的,但我已经将代码更改为C#但是有一些错误。 我不知道这个错误是什么意思。 也许你可以通过其他方式告诉我将文件复制到受保护的文件服务器 用简单的File.Copy(bla bla)它给了我“你没有许可” 当我将VB代码转换为C#时,它给出了以下错误:尝试读取或写入受保护的内存 我找到了解决方案 你可以关注它