如何读取文本文件行并删除第一行?

时间:2015-09-13 22:01:15

标签: c# .net

string[] files = File.ReadAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt");
foreach (string file in files)
{

}

我想从文件UploadedVideoFiles.txt中删除第一行。

4 个答案:

答案 0 :(得分:3)

在这种情况下,使用LINQ是最好的方法:

foreach (string file in files.Skip(1))

答案 1 :(得分:1)

var lines = File.ReadAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt");
File.WriteAllLines(userVideosDirectory + "\\UploadedVideoFiles.txt", lines.Skip(1));

答案 2 :(得分:0)

使用Enumerable.Skip Linq扩展方法:

var List = React.createClass({
  getInitialState: function() {
      return {
          // added example items for demonstration
          items: [
            {name: 'Tom', age: 21},
            {name: 'Bill', age: 44}
          ]
      }
  },
  handleItemChange: function(index, itemData) {
      console.log(index, itemData);
  },
  renderItems: function() {
      // used to get the handleItemChange method inside the map function
      var listComponent = this;

      return this.state.items.map(function(item, i) {
          return (
              <Item
                  {...item}
                  // pass the data and index to the item component
                  // we can then return them later
                  itemData={item}
                  index={i}
                  onChange={listComponent.handleItemChange}
                  // Each child in an array or iterator should have a unique "key" prop
                  key={i}
              />
          );
      });
  },
  render: function() {
      return (
          <div>{this.renderItems()}</div>
      );
  }
});
var Item = React.createClass({
  returnData: function() {
    // pass the props to the onChange function
    this.props.onChange(this.props.index, this.props.itemData);
  },
  render: function() {
      return (
        <div>
          <p>Name: <input
              type="text"
              defaultValue={this.props.name}
              onChange={this.returnData}
          /></p>
          <p>Age: <input
              type="text"
              defaultValue={this.props.age}
              onChange={this.returnData}
          /></p>
        </div>
      );
  }
});

答案 3 :(得分:0)

旧学校(没有LINQ)将开始在索引1处进行迭代。

for (int i = 1; i < files.Length; ++i)
{
   // do something with files[i], which is a line in the file.
}
相关问题