簡體   English   中英

C#進程無法訪問文件''',因為它正由另一個進程使用

[英]C# The process cannot access the file ''' because it is being used by another process

代碼片段只是將字符串寫入名為“all_results.txt”的文本文件中。 我在File.WriteAllText中實現了錯誤。 在網上搜索解決方案之后,我嘗試使用FileStream和StreamWriter作為替代品。 問題仍然存在。

它給了我:

IOException未處理:進程無法訪問文件'C:\\ Users \\ MadDebater \\ Desktop \\ ConsoleTest1 \\ ConsoleTest \\ bin \\ Debug \\ all_results.txt',因為它正由另一個進程使用。

奇怪的是,錯誤是隨意發生的。 它可能是在第3次循環或第45次循環之前遇到錯誤。 我提供了該類的完整代碼,以防問題比看上去更深。 我確定它與我的病毒掃描程序或其他任何東西無關。

try
                {
                    using (FileStream stream = new FileStream(@"all_results.txt", FileMode.Create)) // Exception here
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.WriteLine(result);
                        writer.Dispose();
                        writer.Close();
                    }

                    stream.Dispose();
                    stream.Close();
                }

                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex);
                }

即使我嘗試這個,它仍然失敗。

try
                {
                    File.WriteAllText(@"all_results.txt", result); // Exception here
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                }

以下是該課程的完整代碼。 它旨在接收Twitter推文列表,並逐個使用貝葉斯分類對它們進行分類。

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using BayesClassifier;
    using System.Text.RegularExpressions;

    namespace ConsoleTest
    {
        class Analyzer
        {
            public static void Analyzing(List<string> all_results)
            {

            Reducting(all_results);
            Classifying();
        }

        public static void Reducting(List<string> all_results)
        {
            //Reductor
            //Precondition: List<string> results
            all_results.ForEach(delegate(String text)
            {

                const string ScreenNamePattern = @"@([A-Za-z0-9\-_&;]+)";
                const string HashTagPattern = @"#([A-Za-z0-9\-_&;]+)";
                const string HyperLinkPattern = @"(http://\S+)\s?";
                string result = text;

                if (result.Contains("http://"))
                {
                    var links = new List<string>();
                    foreach (Match match in Regex.Matches(result, HyperLinkPattern))
                    {
                        var url = match.Groups[1].Value;
                        if (!links.Contains(url))
                        {
                            links.Add(url);
                            result = result.Replace(url, String.Format(""));
                        }
                    }
                }

                if (result.Contains("@"))
                {
                    var names = new List<string>();
                    foreach (Match match in Regex.Matches(result, ScreenNamePattern))
                    {
                        var screenName = match.Groups[1].Value;
                        if (!names.Contains(screenName))
                        {
                            names.Add(screenName);
                            result = result.Replace("@" + screenName,
                               String.Format(""));
                        }
                    }
                }

                if (result.Contains("#"))
                {
                    var names = new List<string>();
                    foreach (Match match in Regex.Matches(result, HashTagPattern))
                    {
                        var hashTag = match.Groups[1].Value;
                        if (!names.Contains(hashTag))
                        {
                            names.Add(hashTag);
                            result = result.Replace("#" + hashTag,
                               String.Format(""));
                        }
                    }
                }

                // Write into text file
/*
                try
                {
                    using (FileStream stream = new FileStream(@"all_results.txt", FileMode.Create)) // Exception here
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.WriteLine(result);
                        writer.Dispose();
                        writer.Close();
                    }

                    stream.Dispose();
                    stream.Close();
                }

                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex);
                }
                */
                try
                {
                    File.WriteAllText(@"all_results.txt", result); // Exception here
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                }

            });
        }

        public static void Classifying()
        {
            // Classifying

            BayesClassifier.Classifier m_Classifier = new BayesClassifier.Classifier();


            m_Classifier.TeachCategory("Positive", new System.IO.StreamReader("POSfile.txt"));
            m_Classifier.TeachCategory("Negative", new System.IO.StreamReader("NEGfile.txt"));

            Dictionary<string, double> newscore;
            newscore = m_Classifier.Classify(new System.IO.StreamReader("all_results.txt"));

            PrintResults(newscore);
}

        public static void PrintResults(Dictionary<string, double> newscore)
        {
            foreach (KeyValuePair<string, double> p in newscore)
            {
                Console.WriteLine(p.Key + ", " + p.Value);
            }

            List<string> list = new List<string>();
            using (StreamReader reader = new StreamReader("all_results.txt"))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    list.Add(line);          // Add to list.
                    Console.WriteLine(line); // Write to console.

                }

                reader.Close();
            }

            //PrintSentiment(newscore);
        }

        public static void PrintSentiment(Dictionary<string, double> newscore)
        {

            // if difference < 2, neutral
            // if neg < pos, pos
            // if pos < neg, neg

            double pos = newscore["Positive"];
            double neg = newscore["Negative"];
            string sentiment = "";

            if (Math.Abs(pos - neg) < 1.03)
            {
                sentiment = "NEUTRAL";
            }
            else
            {
                if (neg < pos)
                {
                    sentiment = "POSITIVE";
                }
                else if (pos < neg)
                {
                    sentiment = "NEGATIVE";
                }
            }

            Console.WriteLine(sentiment);


            // append tweet_collection to final_results <string> list
            // append sentiment tag to the final_results <string> list
            // recursive
        }
    }
}

不要在FileStream和StreamWriter上調用Dispose()和Close(),這將由using子句自動處理。

使用像filemon這樣的實用程序來檢查哪些進程正在使用該文件。

更新:從我讀到的過程監視器與filemon非常相似。 從這些工具中的任何一個,您都可以找到哪個進程在何時訪問了您的文件。 您可以在開始監視之前為文件添加過濾器。

您可以嘗試的另一件事是在文件存在時獲取鎖定。

也許文件是由病毒掃描程序或Windows索引服務訪問的?

我遇到類似的問題時發現了帖子。 給出的建議給了我一個想法! 所以為此我編寫了以下方法

public static void ExecuteWithFailOver(Action toDo, string fileName)
{

    for (var i = 1; i <= MaxAttempts; i++)
    {
        try
        {
            toDo();

            return;
        }
        catch (IOException ex)
        {
            Logger.Warn("File IO operation is failed. (File name: {0}, Reason: {1})", fileName, ex.Message);
            Logger.Warn("Repeat in {0} milliseconds.", i * 500);

            if (i < MaxAttempts)
                Thread.Sleep(500 * i);
        }
    }

    throw new IOException(string.Format(CultureInfo.InvariantCulture,
                                        "Failed to process file. (File name: {0})",
                                        fileName));

}

然后我按以下方式使用了該方法

    Action toDo = () =>
                   {
                       if (File.Exists(fileName))
                           File.SetAttributes(fileName, FileAttributes.Normal);

                       File.WriteAllText(
                           fileName,
                           content,
                           Encoding.UTF8
                           );
                   };

    ExecuteWithFailOver(toDo, fileName);

后來我分析了日志,我發現我遇到麻煩的原因是試圖對並行線程中的同一個文件進行操作。 但我仍然看到一些使用建議的FailOver方法的專業人士

嘗試使用鎖定文件操作。 http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

嘗試將文件寫入調試文件夾之外的另一個目錄。

只是一個“瘋狂的鏡頭” - 如果你把文件放在一個更可預測的位置,比如'c:\\ all_results.txt',它會有幫助嗎?

嘗試將Thread.Sleep(1000)放入循環中。 像上面提到的那樣,文件並不總是及時發布,以便循環的下一次迭代。

正如其他人所說,重復打開和關閉文件可能是個問題。 未提及的一種解決方案是在處理期間保持文件打開。 完成后,可以關閉該文件。

佩德羅:

正如其他人所說,重復打開和關閉文件可能是個問題。 未提及的一種解決方案是在處理期間保持文件打開。 完成后,可以關閉該文件。

或者,或者,在StringBuilder或其他內存中的文本存儲中收集文本,然后在循環結束后將文本轉儲到文件中。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM