2014-05-13 3 views
-1

내 컴퓨터를 호스팅하는 응용 프로그램과 같은 클라우드 데이터베이스 서버가 있습니다. 그러나 사용자가 데이터를 저장하려고 할 때마다 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('#')); 
} 
+4

예외를 throw하는 행을 나타 내기 위해 질문을 업데이트 한 경우 도움이됩니다. –

+0

지정된 위치에 파일 또는 디렉토리를 생성하고 권한이 있는지 여부를 확인하십시오. – Hassan

+0

나는 그 디렉토리에 대한 권리가있다.그리고 나는 내 코드 (C# 라이브러리 코드에서와 같이)에서 하나를 보여주지 않는 줄을 모른다. 실제로 그것은 작동하지만 대부분은 실패합니다. – DELTA12

답변

0

자세한 정보없이 구체적인 도움을 제공하기가 어렵습니다. 다음은 몇 가지 문제 해결 제안 사항입니다.

1) 다른 컴퓨터에서 코드를 실행 해보십시오. 특히 개발 컴퓨터. 여전히 같은 오류가 있습니까? 그렇지 않다면 정말로 허가 문제가 있습니다.

2) 예외의 스택 추적을 확인해 보셨습니까?

사용자 컴퓨터에서 응용 프로그램을 실행할 때 IDE를 사용하여 예외를 표시하십시오. 예, 궁극적으로 문제는 하위 수준의 클래스에있을 수 있지만 코드에서 오류를 던지는 메서드를 확인하려면 오류를 중단하고 호출 스택으로 돌아갈 수 있어야합니다.

3) 시스템 수준 예외의 경우에도 실제 예외를 확인하십시오.

IDE에서이 코드를 디버깅 할 수있는 경우 힌트를 제공 할 속성 정보가 표시 될 가능성이 있습니다. 디렉토리 메소드 또는 파일 쓰기 메소드에 있습니까? 추가 속성을 확인하십시오. 어딘가에 경로의 텍스트를 줄 수도 있습니다 (파일 문제라고 가정).

4) 코드

이 엄지 손가락의 좋은 규칙입니다에 예외 처리를 추가, 그리고 당신이 정말로 강한 프로그램을 만들어이 어쨌든을 수행해야합니다. 호출하는 메소드 (사용자, 다른 사람 또는 시스템 메소드)에 관계없이 처리해야하는 위치를 결정해야합니다. 방법에서 "주"에 을에 "충돌 및 연소"예외 처리기를 추가)

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 :

예를 들어, RegisterNewUser() 메소드의 코드에서, 같은 것을 고려 그게 "최고 방법"입니다 (디스크에 쓰려고 시도하는 방법이 거의 없기 때문에 제공 한 스 니펫에서 말하기가 어렵습니다). try-catch 블록에 코드를 래핑하고 예외를 인쇄하거나 쓰기를 수행 할 수 있습니다 디스크.

디스크에 예외를 작성하는 데 문제가있는 경우 먼저 오류 파일을 만들고 해당 프로그램을 실행중인 사용자 계정이 쓰기 권한이 있는지 확인한 다음 catch 블록에서 파일을 엽니 다. APPEND. 이렇게하면 오류 텍스트를 쉽게 얻을 수 있습니다.

6) 다른 모든 것이 실패하면 Debug 클래스 또는 Console 클래스를 사용하여 전통적인 "I made to it line x"를 작성하십시오.

이 방법으로 문제가 해결되지는 않지만 코드가 오류의 원인을 파악하는 데 도움이되는 정보를 얻는 데 도움이됩니다.

+0

정말 고마워요.하지만 등록은 완전히 잘됩니다. 임씨는 데이터의 낭독/저장에 문제가 있음 – DELTA12

관련 문제