Unauthorizedaccessexception

时间:2014-05-13 17:48:27

标签: c# exception io

我的计算机上有一个类似应用程序的云数据库服务器,我正在托管我的游戏。但是,每次用户尝试保存数据时,都会收到UnauthorizedAccessException。 我是由管理员运行它,我在我的文件夹中没有任何specias所以我不知道是什么问题。 这是我的代码:

public const string root = "D:/DATABASE/";
public static void WriteData(string playername, string type, string data)
{
    if (!Directory.Exists("D:/DATABASE/" + playername))
    {
        Directory.CreateDirectory("D:/DATABASE/" + playername);
        Directory.CreateDirectory("D:/DATABASE/" + playername + "/weapons");
    }
    if (type != "Weapon")
    {
        using (StreamWriter sw = new StreamWriter("D:/DATABASE/" + playername + "/" + type + ".sav"))
        {
            sw.WriteLine(data);
        }
    }
    else
    {
        string[] dat = data.Split('%');
        using (StreamWriter sw = new StreamWriter("D:/DATABASE/" + playername + "/weapons/" + dat[0] + ".gfa"))
        {
            string[] lines = dat[1].Split('@');
            foreach (string cline in lines)
            {
                sw.WriteLine(cline);
            }
        }
    }
}
public static string ReadLoadout(string playername)
{
    string output = "";
    string[] items = new string[2];
    using (StreamReader sr = new StreamReader(root + playername + "/loadout.gfl"))
    {
        items[0] = sr.ReadLine();
        items[1] = sr.ReadLine();
    }
    int c = 0;
    foreach (string citem in items)
    {
        if (c > 0) output += "$";
        output += citem + "%" + GetCompressedWeaponFile(playername, citem);
        c++;
    }
    return output;
}

public static string GetCompressedWeaponFile(string playerName, string weaponName)
{
    string output = "";

    using (StreamReader sr = new StreamReader(root + playerName + "/weapons/" + weaponName))
    {
        string line = " ";
        int c = 0;
        while (line != null)
        {
            line = sr.ReadLine();
            if (line != null)
            {
                if (c > 0) output += "@";
                output += line;
            }
            c++;
        }
    }
    return output;
}
public static void RegisterNewUser(string username, string password, string email)
{
    string udir = root + username;
    Directory.CreateDirectory(udir);
    Directory.CreateDirectory(udir + "/weapons");
    Directory.CreateDirectory(udir + "/loadouts");
    File.WriteAllText(udir + "/password.sav", password);
    File.WriteAllText(udir + "/level.sav", "1");
    File.WriteAllText(udir + "/money.sav", "1000");
    File.WriteAllText(udir + "/email.sav", email);
    File.WriteAllText(udir + "/loadout.gfl", "");
    using (StreamWriter sw = new StreamWriter(root + "emails.txt", true))
    {
        sw.WriteLine(email);
    }
    Email.Send(email, "New Account Registration", string.Format(mailTemplate, username, password));
}
public static void EditLoadout(string username, string items)
{
    File.WriteAllLines(root + username + "/loadout.gfl",items.Split('#'));
}

1 个答案:

答案 0 :(得分:0)

如果没有更多信息,很难提供具体的帮助。以下是一些故障排除建议:

1)尝试在不同的计算机上运行代码。特别是你的开发电脑。你还有同样的错误吗?如果没有,那么确实存在许可问题。

2)您是否尝试检查异常的堆栈跟踪?

在自己的计算机上运行应用程序时,请尝试使用IDE显示异常。是的,问题最终可能在低级别的类中,但您应该能够打破错误并返回调用堆栈,以查看代码中的哪个方法实际上是在抛出错误。

3)检查实际异常,即使是系统级异常。

如果您能够在IDE中对此进行调试,则可能会看到将提供提示的属性信息。它是在目录方法还是文件写入方法?检查其他属性。在某个地方它可能会给你路径的文本(假设它是一个文件问题),它失败了,这也可以帮助缩小范围。

4)为代码添加异常处理

这是一个很好的经验法则,无论如何你都应该这样做,以制定更强大的计划。无论您使用何种方法(您的方法,其他人或系统方法),您都需要确定应该在哪里处理。

例如,在您的代码中,在RegisterNewUser()方法中,请考虑以下内容:

 public static void RegisterNewUser(string username, string password, string email)
{
    try
    {
        string udir = root + username;
        Directory.CreateDirectory(udir);
        Directory.CreateDirectory(udir + "/weapons");
        Directory.CreateDirectory(udir + "/loadouts");
        File.WriteAllText(udir + "/password.sav", password);
        File.WriteAllText(udir + "/level.sav", "1");
        File.WriteAllText(udir + "/money.sav", "1000");
        File.WriteAllText(udir + "/email.sav", email);
        File.WriteAllText(udir + "/loadout.gfl", "");
        using (StreamWriter sw = new StreamWriter(root + "emails.txt", true))
        {
            sw.WriteLine(email);
        }
        Email.Send(email, "New Account Registration", string.Format(mailTemplate, username, password));
    }
    catch (Exception ex)
    {
        // Create a method to display or log the exception, with it's own error handler
        LogAndDisplayExceptions(ex);
        // Send the user a message that we failed to add them. Put this in it's own try-catch block
        // ideally, for readability, in it's own method.

        try
        {
            Email.Send(email, "Failed to register", "An error occurred while trying to add your account.");
        }
        catch (Exception exNested)
        {
            LogAndDisplayExceptions(exNested);
        }
    }
}

5)添加"崩溃和烧伤"异常处理程序到" main" 在您的" top方法" (在您提供的代码片段中很难分辨,因为很少有方法会尝试写入磁盘)您可以将代码包装在try-catch块中并打印异常或将其写入磁盘。 / p>

如果您在将异常写入磁盘时遇到问题,我建议您首先创建一个错误文件,确保运行该程序的用户帐户可以写入该文件,然后在catch块中打开文件以获取APPEND 。这样可以更容易地获取错误文本。

6)当所有其他方法都失败时,使用Debug类或Console类来编写传统的"我将它设置为第x行。"

虽然这不能解决您的问题,但它可以帮助您获取更多信息,以便更深入地了解代码导致错误的位置。