繁体   English   中英

无法打开文件 C#

[英]Cannot open file c#

有人能告诉我为什么我在下面的代码中有这个错误吗?

过程无法访问文件“...”,因为它正在被另一个过程使用。

我关闭了第一个StreamReader之后,当我初始化StreamWriter ,它崩溃了。

private static void removeSetting(string _class)
{
    try
    {
        string[] allSettings = new string[20];
        int iSettings = 0;

        using (StreamReader FILE_READER = new StreamReader("DATA.properties"))
        {
            string line = FILE_READER.ReadLine();

            while (line != null)
            {
                if (!line.Equals(""))
                {
                    allSettings[iSettings] = line;
                    iSettings++;
                }
                line = FILE_READER.ReadLine();
            }
        }

        using (StreamWriter FILE_WRITER = new StreamWriter("DATA.properties", false))
        {
            for (int i = 0; i <= iSettings; i++)
            {
                if (!allSettings[i].Split('=')[0].Equals(_class))
                {
                    FILE_WRITER.WriteLine('\n' + allSettings[i] + '\n');
                    //i--;
                }
            }
        }
    }
    catch (Exception ex)
    {
    }
}

public static void saveSetting(string _class, string value)
{
    removeSetting(_class);

    try
    {
        StreamWriter FILE_WRITER = new StreamWriter("DATA.properties", true);

        FILE_WRITER.WriteLine( _class.ToString() +'='+ value);
        FILE_WRITER.Close();
    }
    catch (Exception ex)
    {
    }
}

解决方案:您需要找到一个打开此文件并杀死它或重新加载操作系统的程序。

我建议您像这样使用 _class 来更改“_class=newValue”上的“_class=oldValue”

旧文:

狗=锁

猫=鲍勃

使用 saveSetting(dog, fork)

新文本:

猫=鲍勃

狗=叉子

你可以用这个方法

private static void removeSetting(string _class,string value)
        {
            try
            {
                //read all lines from file
                string[] allSettings = File.ReadAllLines("DATA.properties");

                //divide string on string[] by '='
                List<List<string>> strings = allSettings.Select(x => x.Split(new char[] { '=' }, StringSplitOptions.RemoveEmptyEntries).ToList()).ToList();

                //find all lines where first element equals _class
                List<List<string>> result = strings.Where(x => x.First().Equals(_class)).Select(x=>new List<string>{_class, value}).ToList();

                // convert string[] => string
                string[] secondResult = result.Select(x => String.Join("=",x.ToArray())).ToArray();

                List<List<string>> otherResult = strings.Where(x => !x.First().Equals(_class)).Select(x => x).ToList();

                string[] firstResult = otherResult.Select(x => String.Join("=", x.ToArray())).ToArray();

                //wrtie all lines in file
                File.WriteAllLines("DATA.properties",firstResult);

                File.AppendAllLines("DATA.properties", secondResult);
            }
            catch (Exception ex)
            {

            }
        }

暂无
暂无

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

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