为什么我有时会收到此错误:位置不可用
本文关键字:错误 位置 为什么 | 更新日期: 2023-09-27 18:27:28
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);
}
}
}
}
我以管理员身份从视觉工作室 2012 专业版运行我的程序。我以管理员的身份运行视觉工作室。
例如,现在异常错误在线:
Directory.CreateDirectory(outputtext);
UnauthorizedAccessException对路径"C:''Users''bout0_000''AppData''Local''OutputFiles''outputtext'的访问被拒绝。
还有窗口,它自己在屏幕中间扔给我一个窗口说:位置不可用,然后在里面我看到描述以检查网络以检查我是否插入了磁盘......等。
- 如果我以管理员身份运行我的程序,为什么我没有访问权限? 是否有任何目录,
- 当它在管理员权限下时,我可以使用这些目录可以访问任何用户?或者也许没有管理员权限? 取而代之的是:
C:''Users''bout0_000''AppData''Local''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:
你的代码中有一些错误,试试这个修改后的版本:
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)
删除将删除所有子目录和文件。因此,删除outputfiles
文件夹并创建一个新文件夹后,所有子文件夹(outputphotos
,outputtext
,...(显然都不存在。这意味着,我们不必检查子文件夹是否存在,只需创建新文件夹即可。相同的逻辑也适用于outputphotos
文件夹。
在创建 - 子目录之前,您不必先创建父目录。
Directory.CreateDirectory(path)
将在创建子目录(path
中的最后一个(之前创建所需的所有父目录。