访问文件夹拒绝的C#

时间:2018-11-25 16:33:57

标签: c# access-denied

我是一个相当新的C#程序员,但遇到了我无法修复的错误。

当前,我正在编写一个discord bot的代码,并尝试实例化Program对象时,它返回“访问被拒绝错误”。 问题在于错误是指向文件夹而不是文件,我已经尝试了很多方法对其进行修复。

  • 以管理员身份运行Visual Studio
  • 确保我的帐户有权访问文件和文件夹
  • 更改项目文件的位置
  • 重新启动Visual Studio
  • 重新编写项目
  • 确保文件夹和文件不是“只读”

此行引发错误:=> new Program().MainAsync().GetAwaiter().GetResult();

我现在基本上没有主意了。异常消息的详细信息如下:

  

System.UnauthorizedAccessException     HResult = 0x80070005     消息=拒绝访问路径'C:\ Users \ XXX \ source \ repos \ discordBot \ discordBot \ bin \ Debug'。     来源= mscorlib     堆栈跟踪:      在System.IO .__ Error.WinIOError(Int32 errorCode,可能是StringFullPath)      在System.IO.FileStream.Init(字符串路径,FileMode模式,FileAccess访问,Int32权限,布尔useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,字符串msgPath,布尔bFromProxy,布尔useLongPath,布尔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
        }
    }
}

2 个答案:

答案 0 :(得分:3)

问题可能是您尝试打开目录作为文件。您构造的路径是:

Path.GetDirectoryName(Assembly.GetEntryAssembly().Location).Replace(@"bin\Debug\netcoreapp2.0", @"Token.txt")

这仅在Assembly.GetEntryAssembly()。Location确实包含字符串@“ bin \ Debug \ netcoreapp2.0”时有效。

您可能想要类似

Path.Combine(Path.GetDirectoryName(Assembly.GetEntryAssembly().Location)), @"Token.txt")

答案 1 :(得分:0)

就像错误提示一样,您无权访问该文件夹。如果在调试模式下运行它,请确保以管理员身份运行Visual Studio,以便忽略开发环境上的所有内容。如果已部署,请确保运行程序的帐户对该文件夹具有适当的权限。

相关问题