繁体   English   中英

访问文件夹拒绝的C#

[英]Access to folder Denied C#

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

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

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

在此行抛出错误: => new Program().MainAsync().GetAwaiter().GetResult();

在这一点上,我基本上没有主意。 异常消息的详细信息如下:

System.UnauthorizedAccessException HResult = 0x80070005 Message =拒绝访问路径'C:\\ Users \\ XXX \\ source \\ repos \\ discordBot \\ discordBot \\ bin \\ Debug'。 Source = mscorlib StackTrace:位于System.IO .__ Error.WinIOError(Int32 errorCode,字符串可能是FullPath)位于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的train_bot.Program.d__3.MoveNext()中: \\ Users \\ XXX \\ source \\ repos \\ discordBot \\ discordBot \\ Program.cs:System 46中System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(任务任务)的System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(任务任务)的Line 46。 C:\\ Users \\ XXX \\ source \\ repos \\ discordBot \\ discordBot \\ Program.cs:line 21中train_bot.Program.Main(String [] args)处的Runtime.CompilerServices.TaskAwaiter.GetResult()

不太详细的版本

'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
        }
    }
}

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

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")

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM