【问题标题】:Access to the path 'C:\Users\mehdi\Desktop\sample\Test' is denied拒绝访问路径“C:\Users\mehdi\Desktop\sample\Test”
【发布时间】:2016-09-13 20:19:27
【问题描述】:

我正在尝试将文件夹加载到 winForm 应用程序中。应用程序应该读取文件夹中的文件并对文件执行一些操作。以下是实现:

private void button1_Click(object sender, EventArgs e)
    {
        FolderBrowserDialog folderBrowserDialog = new FolderBrowserDialog();
        DialogResult result = folderBrowserDialog.ShowDialog();

        var files = from file in Directory.EnumerateFiles(folderBrowserDialog.SelectedPath, "*.chunk*", SearchOption.AllDirectories)
                    from line in File.ReadLines(file)
                    select new
                    {
                        File = file,
                        Line = line
                    };
        string newPath = folderBrowserDialog.SelectedPath;
        if (!Directory.Exists(newPath))
        {
            System.IO.Directory.CreateDirectory(newPath + @"\Test");
        }

        foreach (var f in files)
        {

            string path = f.File.ToString();
            string filename = Path.GetFileName(path);
            string s = string.Empty;
            using (StreamReader reader = new StreamReader(path, true))
            {
                s = reader.ReadToEnd();
                reader.Close();
            }

            string[] parts = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);

            using (File.CreateText(Path.Combine(newPath + @"\Test", filename+".txt")))
            { }
            using (StreamWriter sw = File.CreateText(Path.Combine(newPath + @"\Test", filename + ".txt")))
            {
                string output = string.Empty;
                foreach (string st in parts)
                {
                    output += st + ",";
                }
                sw.Write(output);
            }
        }
    }

using (File.CreateText(newPath)) 行有错误提示:

在 mscorlib.dll 中发生了“System.UnauthorizedAccessException”类型的未处理异常

附加信息:对路径“C:\Users\mehdi\Desktop\sample\Test”的访问被拒绝。

我想要做的是,应用程序应该加载文件夹并读取每个文本文件并执行给定的任务(用逗号替换单词之间的无序空格),然后将每个文件保存在新的文件夹创建为System.IO.Directory.CreateDirectory(newPath);。一切都按预期进行,但是当操作达到将文件写入新创建的文件夹Test 时,它不允许访问。

任何想法我哪里出错了?

【问题讨论】:

  • 只是为了确认一下:当您将路径复制到资源管理器中时,文件夹打开时不会出现问题或提示,并且您的应用程序正在运行管理员凭据,对吗?
  • @ManfredRadlwimmer。文件夹在资源管理器中打开正常。应用程序正在管理员凭据下运行。谢谢
  • @ManfredRadlwimmer 我已经编辑了我的问题。请再试一次?谢谢
  • @ManfredRadlwimmer 除了这行代码output += st + ","; 之外,其他一切都运行良好,当您在进程中移动时,大文件会减慢进程。你能改进它吗?
  • 由于您的请求远远超出了最初的问题,因此不太可能使其他有类似问题的人受益。由于原始问题已解决,因此应该投票并接受@vendettamit 的回答。如果您(当然是在自己尝试之后)有另一个问题(如何在替换大量文本时提高性能),您应该打开另一个问题并应用您在此处学到的知识。

标签: c# winforms


【解决方案1】:

您正在使用newPath 中的目录路径来创建文件。在newPath 中附加文件名以创建文件。

File.CreateText(Path.Combine(newPath, "<yourfileName>.extension"))

【讨论】:

  • 感谢您的回答。你能帮忙从上面的代码中提取file name吗?
  • 您的意思是所选目录中的文件名吗?检查string path = f.File.ToString();是一样的。
  • 做到了。执行已经超过了那行代码。但是我们现在被抛出到using (StreamWriter sw = File.CreateText(newPath)) 行,错误为Additional information: Could not find a part of the path 'C:\Users\mehdi\Desktop\sample\'.
  • 在此行也重复相同的解决方案。该代码具有与答案中所述相同的错误。
  • 我们也遇到了这个错误。执行现在卡在using (StreamWriter sw = File.CreateText(newPath + @"\Test")) 行,错误消息为An unhandled exception of type 'System.UnauthorizedAccessException' occurred in mscorlib.dll Additional information: Access to the path 'C:\Users\mehdi\Desktop\sample\Test' is denied.
猜你喜欢
  • 2011-10-28
  • 1970-01-01
  • 2011-03-29
  • 2010-09-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-04-02
  • 2010-11-29
相关资源
最近更新 更多