为什么我有时会收到此错误:位置不可用?

时间:2013-08-13 00:32:55

标签: c# windows winforms

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.IO;

namespace Diagnostic_Tool_Blue_Screen
{

    public static class CreateDirectories
    {

        static string outputfiles;
        static string photofilesDir;
        static string photofilesDir1;
        static string photofilesDir2;
        static string photofilesDir3;
        static string temptxt;
        static string tempphotos;
        static string outputtext;
        static string outputphotos;

        public static void CreateDirectories()
        {

            outputfiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OutputFiles");
            if (!Directory.Exists(outputfiles))
            {
                Directory.CreateDirectory(outputfiles);
            }
            else
            {
                Directory.Delete(outputfiles, true);
                Directory.CreateDirectory(outputfiles);
            }
            outputtext = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + outputfiles + "\\outputtext";
            if (!Directory.Exists(outputtext))
            {
                Directory.CreateDirectory(outputtext);
            }
            else
            {
                Directory.Delete(outputtext, true);
                Directory.CreateDirectory(outputtext);
            }
            outputphotos = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + outputfiles + "\\outputphotos";
            if (!Directory.Exists(outputphotos))
            {
                Directory.CreateDirectory(outputphotos);
            }
            else
            {
                Directory.Delete(outputphotos, true);
                Directory.CreateDirectory(outputphotos);
            }
            temptxt = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + outputfiles + "\\txtfiles";
            if (!Directory.Exists(temptxt))
            {
                Directory.CreateDirectory(temptxt);
            }
            else
            {
                Directory.Delete(temptxt, true);
                Directory.CreateDirectory(temptxt);
            }
            tempphotos = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\photosfiles";
            if (!Directory.Exists(tempphotos))
            {
                Directory.CreateDirectory(tempphotos);
            }
            else
            {
                Directory.Delete(tempphotos, true);
                Directory.CreateDirectory(tempphotos);
            }


            photofilesDir = outputphotos + "\\" + "photosfiles";
            if (!Directory.Exists(photofilesDir))
            {
                Directory.CreateDirectory(photofilesDir);
            }
            else
            {
                Directory.Delete(photofilesDir, true);
                Directory.CreateDirectory(photofilesDir);
            }
            photofilesDir1 = outputphotos + "\\" + "photosfiles1";
            if (!Directory.Exists(photofilesDir1))
            {
                Directory.CreateDirectory(photofilesDir1);
            }
            else
            {
                Directory.Delete(photofilesDir1, true);
                Directory.CreateDirectory(photofilesDir1);
            }
            photofilesDir2 = outputphotos + "\\" + "photosfiles2";
            if (!Directory.Exists(photofilesDir2))
            {
                Directory.CreateDirectory(photofilesDir2);
            }
            else
            {
                Directory.Delete(photofilesDir2, true);
                Directory.CreateDirectory(photofilesDir2);
            }
            photofilesDir3 = outputphotos + "\\" + "photosfiles3";
            if (!Directory.Exists(photofilesDir3))
            {
                Directory.CreateDirectory(photofilesDir3);
            }
            else
            {
                Directory.Delete(photofilesDir3, true);
                Directory.CreateDirectory(photofilesDir3);
            }
        }
    }
}

我从visual studio 2012专业版运行我的程序作为管理员。我以管理员的身份运行了visual studio。

例如,现在异常错误就行了:

Directory.CreateDirectory(outputtext);

UnauthorizedAccessException 访问路径'C:\ Users \ bout0_000 \ AppData \ Local \ OutputFiles \ outputtext'被拒绝。

还有windows它自己给我一个窗口中间的窗口说:位置不可用然后在里面我看到描述检查网络检查我是否插入了磁盘....等等。

  1. 如果我以管理员身份运行我的程序,为什么我无法访问?
  2. 在管理员权限不足的情况下,我可以使用哪些目录可以访问任何用户?或者可能没有管理员权限?取而代之的是:
  3. C:\用户\ bout0_000 \应用程序数据\本地\ OutputFiles \的outputText

    也许在C:\ Users \ bout0_000 \ AppData \ Local中还有其他位置根本不需要管理员?也许C:\ Users \ bout0_000 \ AppData \ Local \ Temp?

    这是完整的异常错误:

    System.UnauthorizedAccessException was unhandled
      HResult=-2147024891
      Message=Access to the path 'C:\Users\bout0_000\AppData\Local\OutputFiles\outputtext' is denied.
      Source=mscorlib
      StackTrace:
           at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
           at System.IO.Directory.InternalCreateDirectory(String fullPath, String path, Object dirSecurityObj, Boolean checkHost)
           at System.IO.Directory.InternalCreateDirectoryHelper(String path, Boolean checkHost)
           at Diagnostic_Tool_Blue_Screen.CreateDirectories.CreateDirectoriesAtConstructor() in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\CreateDirectories.cs:line 40
           at Diagnostic_Tool_Blue_Screen.Form1..ctor() in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Form1.cs:line 127
           at Diagnostic_Tool_Blue_Screen.Program.Main() in d:\C-Sharp\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Diagnostic Tool Blue Screen\Program.cs:line 19
           at System.AppDomain._nExecuteAssembly(RuntimeAssembly assembly, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state, Boolean preserveSyncCtx)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()
      InnerException: 
    

1 个答案:

答案 0 :(得分:0)

您的代码中存在一些问题,请尝试修改此版本:

public static void CreateDirectories()
    {

        outputfiles = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData), "OutputFiles");
        if (Directory.Exists(outputfiles)) Directory.Delete(outputfiles, true);            
        outputtext = outputfiles + "\\outputtext";            
        Directory.CreateDirectory(outputtext);

        outputphotos = outputfiles + "\\outputphotos";
        Directory.CreateDirectory(outputphotos);

        temptxt = outputfiles + "\\txtfiles";
        Directory.CreateDirectory(temptxt);

        tempphotos = Environment.GetFolderPath(Environment.SpecialFolder.LocalApplicationData) + "\\photosfiles";
        if (Directory.Exists(tempphotos)) Directory.Delete(tempphotos, true);
        Directory.CreateDirectory(tempphotos);            

        photofilesDir = outputphotos + "\\" + "photosfiles";            
        Directory.CreateDirectory(photofilesDir);

        photofilesDir1 = outputphotos + "\\" + "photosfiles1";
        Directory.CreateDirectory(photofilesDir1);

        photofilesDir2 = outputphotos + "\\" + "photosfiles2";
        Directory.CreateDirectory(photofilesDir2);

        photofilesDir3 = outputphotos + "\\" + "photosfiles3";
        Directory.CreateDirectory(photofilesDir3);
    }

有些说明:

  • 在创建新文件夹之前,您似乎要删除旧的outputfiles文件夹。使用Directory.Delete(path,true)删除将删除所有子目录和文件。因此,删除outputphotos文件夹并创建新文件夹后,所有子文件夹(outputtextoutputfiles,...)显然不存在。这意味着,我们不必检查子文件夹是否存在,只需创建新文件夹即可。同样的逻辑也适用于outputphotos文件夹。
  • 在创建子目录之前,您不必先创建父目录。 Directory.CreateDirectory(path)将在创建子目录(path中的最后一个目录)之前创建所需的所有父目录。