小编use*_*872的帖子

如何在文本文件中上下移动项目

如何在文本文件中上下移动项目/值.目前我的程序读取文本文件,使用一段时间确保在没有更多行要读取时停止.我使用if语句来检查counter是否等于我想要移动的值的行.我不知道怎么从这里继续.

  _upORDown = 1; 

    using (StreamReader reader = new StreamReader("textfile.txt"))
    {
        string line = reader.ReadLine();
        int Counter = 1;
        while (line != null)
        {

            if (Counter == _upORDown)
            {
              //Remove item/replace position

            }
            Counter++;
        }
    }
Run Code Online (Sandbox Code Playgroud)

.net c# visual-studio

5
推荐指数
1
解决办法
775
查看次数

c#设置DataSource属性时,无法修改项集合

我正在尝试将项目向上/向下移动到列表框但是不断收到错误,不知道如何修复它.错误与" listBox1.Items.Remove(selected);"有关

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace company1
{
    public partial class Form1 : Form
    {
        List<Configuration> lines = new List<Configuration>();

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.listBox1.Items.Clear();
            //Read in every line in the file
            using (StreamReader reader = new StreamReader("file.txt"))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    //string textfile = line;
                    string[] myarray = new …
Run Code Online (Sandbox Code Playgroud)

.net c# visual-studio-2010

2
推荐指数
1
解决办法
8275
查看次数

该进程无法访问该文件,因为它正在被使用(错误)

我试图将文本文件中的行向上移动,然后将其重写回原始文件,但由于某种原因而出现错误,似乎无法弄明白.

using (StreamReader reader = new StreamReader("file.txt"))
{
    string line;
    int Counter = 0;

    while ((line = reader.ReadLine()) != null)
    {
        string filepath = "file.txt";
        int i = 5;
        string[] lines = File.ReadAllLines(filepath);

        if (lines.Length >= i)
        {
            string tmp = lines[i];
            lines[i] = lines[i-1];
            lines[i-1] = tmp;
            File.WriteAllLines(filepath, lines);
        }   
    }
    Counter++;
}
Run Code Online (Sandbox Code Playgroud)

.net c# visual-studio c#-4.0

0
推荐指数
1
解决办法
2271
查看次数

标签 统计

.net ×3

c# ×3

visual-studio ×2

c#-4.0 ×1

visual-studio-2010 ×1