简体   繁体   English

C# “文件正在被另一个进程使用”

[英]C# "File is being used by another process"

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

namespace Lists
{
    public partial class Form3 : Form
    {
        public List<string> storeNames = new List<string>();
        //public List<string> data = new List<string>();
        public string fullPath;
        public string[] sr;
        public int counter = 0;
        // Change the path values to where names.txt is located on your computer
        public string pathName = "C:\\Users\\xxxxxxxxxxx\\Desktop";
        public string fileName = "\\names.txt";
        public Form3()
        {
            InitializeComponent();
        }

        private void Form3_Load(object sender, EventArgs e)
        {
            readFileIO(pathName, fileName);
            
        }

        private void listNamesBtn_Click(object sender, EventArgs e)
        {
            MessageBox.Show($"{counter}", "Number of list names");
            return;
        }

        private void addNameBtn_Click(object sender, EventArgs e)
        {
            writeFile(pathName, fileName);
        }
        public void writeFile(string pathName, string fileName)
        {
            string fullPath = pathName + fileName;
            TextWriter txt = new StreamWriter(fullPath, true);
            txt.Write("\n" + Input.Text);
            curNamesBox.Items.Clear();
            readFileIO(pathName, fileName);
            txt.Dispose();
        }


        public void readFileIO(string pathName, string fileName)
        {
            fullPath = pathName + fileName;
            var lines = File.ReadAllLines(fullPath);
            foreach (string line in lines)
            {
                counter++;
                curNamesBox.Items.Add(line);
                
            }
        }

        private void deleteNameBtn_Click(object sender, EventArgs e)
        {
            searchName("meep morp");
            deleteNameFunc();
        }

        public void searchName(string registeredName)
        {
            foreach(string searchName in curNamesBox.Items)
            {
                if (registeredName.Equals(searchName))
                {

                    Console.WriteLine("I FOUND YOU :)");
                    
                }
            }

            string[] data = File.ReadAllLines(fullPath);

            foreach (string line in data)
            {
                Console.WriteLine(line);
                if (line.Equals("meep morp"))
                {
                    
                    Console.WriteLine("cry about it.");
                    
                }
            }
            
        }
        public void deleteNameFunc()
        {
            
            foreach(string name in File.ReadAllLines(fullPath))
            {
                storeNames.Add(name);
            }
            int namesIndex = 0;
            foreach(string x in storeNames.ToList())
            {
                namesIndex++;

                Console.WriteLine(x);
                Console.WriteLine(namesIndex);

                if (x.Equals("meep morp"))
                {
                    storeNames.Remove("Tabitha");
                    removeNCreateFile();
                    
                }
            }
            foreach(string y in storeNames)
            {
                Console.WriteLine(y);
            }
        }

        public void removeNCreateFile()
        {
            File.Delete(fullPath);
            TextWriter txt = new StreamWriter(fullPath, true);
            File.Create(fullPath);
            foreach (string name in storeNames)
            {

                txt.Write("\n" + name);
                
            }
            txt.Dispose();


        }

        private void curNamesBox_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
    }
}

The code is supposed to add a name to the file when the button is clicked, and it is supposed to delete the old file and replace it with the new info in the list box but returns saying the process is already used?该代码应该在单击按钮时为文件添加一个名称,并且应该删除旧文件并将其替换为列表框中的新信息但返回说该过程已被使用?

My code is saying for the line around ' var lines = File.ReadAllLines(fullPath);我的代码说的是 ' var lines = File.ReadAllLines(fullPath);周围的行' near the method ReadFileIO. ' 在 ReadFileIO 方法附近。 Can someone please help me fix this and understand why?有人可以帮我解决这个问题并了解原因吗?

I tried to add a name to the file when the button is clicked and it says that the file is already being used by another process.单击按钮时,我试图为文件添加一个名称,但它说该文件已被另一个进程使用。

The streamwriter is opening the file Streamwriter 正在打开文件

TextWriter txt = new StreamWriter(fullPath, true)

Then it is attempted to open again here然后在这里再次尝试打开

readFileIO(pathName, fileName);

You'll need to either pass the file to readFileIO or close the file stream.您需要将文件传递给 readFileIO 或关闭文件 stream。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM