Tag: 文件

文件未找到例外..但它就在那里

嘿,这将是一个愚蠢的问题。 我试图在我的本地系统上获取一个文件,并且我不断收到FileNotFoundException 。 有人让我直接请:) if( File.Exists(@”C:\logs\hw-healthways-prod_2009-08-26.tar”)) { Console.WriteLine(“Yay”); } else { throw new FileNotFoundException(); } 尝试将文件移动到与正在执行的应用程序相同的位置,并执行以下操作: if( File.Exists(“hw-healthways-prod_2009-08-26.tar”)) 一样。 然后我做了一个随机的txt文件并把它停在那里..“me.txt” 它有效吗?! 那么你的文件名是什么问题?

如何等到File.Exists?

我有一个应用程序,在所选文件夹中侦听* .log文件。 我用过FileSystemWatcher 。 但有一个问题。 负责制作该文件的另一个应用程序采取以下步骤: 制作一个* .gz文件 将其解压缩到txt文件(一些随机文件名) 将* .txt名称更改为具有* .log扩展名的正确名称。 我无法改变这种行为。 所以我为* .gz和* .txt文件制作了2个FileSystemWatcher 。 为什么? 因为此应用程序有时不解压缩gz文件,有时不会将txt文件重命名为最终的* .log文件。 FileSystemWatcher2捕获txt文件,然后(在大多数情况下,它被重命名为在接下来的1000ms内登录)我需要等待一段时间来检查txt文件是否存在(如果没有,它似乎被重命名为最终的* .log文件) 。 问题是,如何在没有Thread.Sleep()情况下检查文件是否存在以防止UI冻结? 我希望很清楚,如果不是,我会尝试更好地描述它。 我认为这是一个复杂的问题。 一些代码示例: Watz for gz文件: private void fileSystemWatcher_Created(object sender, FileSystemEventArgs e) { //this is for gz files in case if gz file is not unpacked automatically by other app //I […]

区分大小写的Directory.Exists / File.Exists

有没有办法让案例敏感的Directory.Exists / File.Exists从那以后 Directory.Exists(folderPath) 和 Directory.Exists(folderPath.ToLower()) 两者都回归true ? 大多数时候它并不重要但我使用的宏如果路径与100%的情况不匹配似乎不起作用。

在C#中读取和写入文本文件时出现问题

好的,所以我查看了与我的问题有关的所有其他问题,看不出我出错的地方。 我的目标是将骰子模拟器的结果写入文本文件,然后将该内容读入C#中的列表框。 这是我到目前为止的代码: namespace WpfApplication1 { public MainWindow() { InitializeComponent(); } private void button1_Click(object sender, RoutedEventArgs e) { try { Random RandomValue = new Random(); Random RandomValue2 = new Random(); using (StreamWriter outputFile = new StreamWriter(“C:\\Example.txt”)) { for (int i = 0; i 100; i++) { toss = inputFile.ReadLine(); listBox1.Items.Add(toss); } catch (Exception ex) { […]

当文件不存在时,为什么File.Exists()会慢得多?

在我看来,当文件不存在或者用户没有访问权时文件存在时,File.Exists()要慢得多。 这是真的? 这对我来说没有意义。

如何使用C#同时读/写表格文件?

我创建了多个服务器。每个服务器都必须将一些数据发送到自己的客户端。 我正在使用TCP / IP协议。 为防止因客户端断开连接而导致数据丢失,我使用文本文件作为缓冲区。 所以在程序中,每个服务器都有一个线程,它继续检查客户端是否连接。 如果它已连接,则它从缓冲区读取并将其发送到客户端。 每当必须将一些新数据发送到客户端时,我首先检查客户端是否已连接。如果客户端未连接,则我将数据写入相同的缓冲区文本文件。 我面临的问题是,当线程从中读取文件时,我无法写入文件。 using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Net; using System.Net.NetworkInformation; using System.Net.Sockets; using System.Text; using System.Threading; using System.Threading.Tasks; namespace WindowsFormsApplication1 { public class TcpIp { public int machinePort; public static int port1 = 1024; public static int count = 0; public string bufferName; FileStream […]

将web api blob读入一个字符串,我可以将其作为json对象的一部分发送到服务器并转回文件

我正在尝试将wav文件转换为字符串,我可以将其作为json对象的一部分发送到服务器,以便在服务器上我可以将该字符串转换回文件。 我曾尝试使用readAsBinaryString并将其作为文本读取,在将字符串读入字节数组时无法获得过去的错误。 reader.onloadend = saveMedia; reader.readAsText(Blob); //reader.readAsBinaryString(Blob); also tried. 然后回调发送一个ajax请求,其中一个对象持有“reader.result”中的字符串,并在服务器上我尝试过如下操作: System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding(); byte[] BinaryData = encoding.GetBytes(stringFromRequest); 下面这个问题的答案似乎是不应该这样做。 但我真的想这样做,因为我使用的另一个工具(微风js)。 不希望对文件数据类型使用单独的post操作。 相关: 文件API – Blob到JSON

如何从此输出中写入文本文件

我有以下代码,以便我可以搜索目录来查找文件。 现在我想为用户添加一种将输出保存到文本文件的方法? using System; using System.Drawing; using System.Collections; using System.ComponentModel; using System.Windows.Forms; using System.Data; using System.IO; namespace RecursiveSearchCS { public class Form1 : System.Windows.Forms.Form { internal System.Windows.Forms.Button btnSearch; internal System.Windows.Forms.TextBox txtFile; internal System.Windows.Forms.Label lblFile; internal System.Windows.Forms.Label lblDirectory; internal System.Windows.Forms.ListBox lstFilesFound; internal System.Windows.Forms.ComboBox cboDirectory; private System.ComponentModel.Container components = null; public Form1() { InitializeComponent(); } protected override […]

查找包含给定字符串的所有行

System.IO.StreamReader file = new System.IO.StreamReader(@”data.txt”); List Spec= new List(); while (file.EndOfStream != true) { string s = file.ReadLine(); Match m = Regex.Match(s, “Spec\\s”); if (m.Success) { int a = Convert.ToInt16(s.Length); a = a – 5; string part = s.Substring(5, a); Spec.Add(part); } } 我正在尝试获取包含单词“Spec”的所有行,然后是空格字符,但是当我运行此程序时出现错误。 例外的细节如下: An unhandled exception of type ‘System.ArgumentOutOfRangeException’ occurred in mscorlib.dll 任何人都可以协助我找出原因吗? […]

C#如何在文本文件中写多行?

我试图按一个按钮,从textbox4和textbox5获取文本将其写入文本文件。 但是,当我再次按下它以向文本文件添加新信息时,它只是将旧文本替换为新文本。 每次按下按钮时,如何让它在第一行下面写另一行? 这是我到目前为止的代码 private void button5_Click(object sender, EventArgs e) { try { xuidspath = @”c:\xuids.txt”; ListViewItem lvi = new ListViewItem(); lvi.Text = textBox4.Text; lvi.SubItems.Add(textBox5.Text); listXuid.Items.Add(lvi); TextWriter xuids = new StreamWriter(xuidspath); xuids.WriteLine(textBox4.Text + “-” + textBox5.Text); textBox5.Clear(); textBox4.Clear(); xuids.Close(); } catch (Exception ex) { MessageBox.Show(ex.Message, “Error!”, MessageBoxButtons.OK, MessageBoxIcon.Error); } } 有任何想法吗?