简体   繁体   中英

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);' near the method 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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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