【发布时间】:2018-11-25 16:33:56
【问题描述】:
我是一个相当新的 C# 程序员,我遇到了一个我无法修复的错误。
目前我正在编写一个不和谐的机器人,并在尝试实例化和Program 对象时,它返回一个“访问被拒绝错误”。
问题是错误指的是文件夹,而不是文件,我已经尝试了很多东西来修复它。
- 以管理员身份运行 Visual Studio
- 确保我的帐户有权访问文件和文件夹
- 更改项目文件的位置
- 重新启动 Visual Studio
- 从零开始重新编码项目
- 确保文件夹和文件不是“只读”
在这一行抛出错误:=> new Program().MainAsync().GetAwaiter().GetResult();
在这一点上,我基本上没有想法。异常消息的完整详细信息如下:
System.UnauthorizedAccessException H结果=0x80070005 消息=拒绝访问路径“C:\Users\XXX\source\repos\discordBot\discordBot\bin\Debug”。 源=mscorlib 堆栈跟踪: 在 System.IO.__Error.WinIOError(Int32 错误代码,字符串可能全路径) 在 System.IO.FileStream.Init(字符串路径、FileMode 模式、FileAccess 访问、Int32 权限、Boolean useRights、FileShare 共享、Int32 bufferSize、FileOptions 选项、SECURITY_ATTRIBUTES secAttrs、String msgPath、Boolean bFromProxy、Boolean useLongPath、Boolean checkHost) 在 System.IO.FileStream..ctor(字符串路径,FileMode 模式,FileAccess 访问) 在 C:\Users\XXX\source\repos\discordBot\discordBot\Program.cs:line 46 中的 train_bot.Program.d__3.MoveNext() 在 System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务) 在 System.Runtime.CompilerServices.TaskAwaiter.GetResult() 在 C:\Users\XXX\source\repos\discordBot\discordBot\Program.cs:line 21 中的 train_bot.Program.Main(String[] args)
不太详细的版本
System.UnauthorizedAccessException: '对路径'C:\Users\SettingAdmin\source\repos\discordBot\discordBot\bin\Debug'的访问被拒绝。'
using System;
using System.IO;
using System.Reflection;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
namespace train_bot
{
class Program
{
private DiscordSocketClient Client;
private CommandService Commands;
static void Main(string[] args)
=> new Program().MainAsync().GetAwaiter().GetResult();
private async Task MainAsync()
{
//configuring client
Client = new DiscordSocketClient(new DiscordSocketConfig
{
LogLevel = LogSeverity.Debug //changes detail in log
});
Commands = new CommandService(new CommandServiceConfig
{
CaseSensitiveCommands = true,
DefaultRunMode = RunMode.Async,
LogLevel = LogSeverity.Debug
});
Client.MessageReceived += Client_MessageReceived;
await Commands.AddModulesAsync(Assembly.GetEntryAssembly());
Client.Ready += Client_Ready;
Client.Log += Client_Log;
string Token = "";
using (var Steam = new FileStream(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location).Replace(@"bin\Debug\netcoreapp2.0", @"Token.txt"), FileMode.Open, FileAccess.Read))using (var ReadToken = new StreamReader(Steam))
{
Token = ReadToken.ReadToEnd();
}
await Client.LoginAsync(TokenType.Bot, Token);
await Client.StartAsync();
await Task.Delay(-1);
}
private async Task Client_Log(LogMessage Message)
{
Console.WriteLine($"{DateTime.Now} at {Message.Source}] {Message.Message}");
}
private async Task Client_Ready()
{
await Client.SetGameAsync("Hentai King 2018", "", StreamType.NotStreaming);
}
private async Task Client_MessageReceived(SocketMessage arg)
{
//Configure the commands
}
}
}
【问题讨论】:
标签: c# access-denied