2009-02-25 4 views

답변

8

사용 Environment.GetFolderPath는 OS에 독립적 인 방식으로 가장 적절한 폴더를 얻을 수 있습니다. 파일이 로밍하는 경우 사용자 별, 단지 응용 프로그램 사용하기위한 것입니다 및 문서를 사용자를 대변하지 않습니다 -

  • ApplicationData : 특히

    다음 SpecialFolder 값 중 하나가 필요합니다 걱정할 수도 있습니다.

  • LocalApplicationData - 파일이 비 로밍, 사용자 별이며 응용 프로그램 전용이며 사용자가 신경 쓸 문서를 나타내지 않는 경우.
  • CommonApplicationData - 파일이 로밍중인 경우 모든 사용자에게 공통적이며 응용 프로그램에서만 사용되며 사용자가 신경 쓸 문서를 나타내지는 않습니다. 참고 : Vista의 경우이 주소는 C:\ProgramData으로 매핑됩니다.이 파일은 기본적으로 읽기 전용이며 일반 사용자의 경우 파일을 변경하면 관리자가 사용하는 프로그램의 동작에 영향을 줄 수 있습니다. 명시 적으로 앱 하위 폴더의 권한을 변경하거나 다른 옵션 중 하나를 선택할 수 있습니다.
  • MyDocuments - 파일이 사용자 별이며 문서를 나타내는 경우.
  • 일 ( C:\Documents and Settings\All Users\Documents XP에와 비스타에 C:\Users\Public\Documents)와 같은 역할을하도록 구성 폴더가 있음에도 불구하고, 기계 전체 문서 저장소를 나타낼 것 CommonDocuments 같은 더 SpecialFolder 열거 값이 없다는 것을

주 . 이 위치에 쓰려면 OS 버전을 직접 찾아 적절한 폴더를 선택해야합니다.

내부적으로 Environment.GetFolderPath은 Win32 API SHGetFolderPath을 사용합니다. SHGetFolderPath에 의해 사용되는 열거 형은 공통 문서를 포함하여 다른 여러 특수 폴더에 대한 잘 알려진 위치를 제공합니다. 직접 SHGetFolderPath을 사용할 수 있습니다. Pluvoke.net에서 p/invoke definition 및 해당 CSIDL enum definition을 찾을 수 있습니다.

IsolatedStorage도 사용할 수 있습니다. 그러나 제한된 할당량으로 사용자별로 이동이 불가능하며 Windows 탐색기에서 사용자가 쉽게 액세스 할 수 없습니다. 따라서 실제로는 SpecialFolder.ApplicationData에 해당하는 중간/낮은 신뢰도입니다.

-2

왜 응용 프로그램 디렉토리 (응용 프로그램에서 설치된 동일 하나)에서?

+0

현재는하지만 프로그램 파일 디렉토리는 표준 사용자, 즉 비 관리자 만 쓸 수 있습니다. –

+0

비스타의 관리자조차 비스타의 허락을 받아 들일 수 있습니다. –

+0

매니 페스트에서 'requestedExecutionLevel level = "requireAdministrator"'또는 level = "highestAvailable"을 설정하여이 문제를 해결할 수 있지만 여전히 권장되지 않으며 최선의 방법은 아닙니다. – LarryF

0

응용 프로그램 데이터를 사용자별로 지정 하시겠습니까? 그렇다면 C:\Users\%username%\%appname%\...에 넣을 것을 고려해야합니다. 그렇지 않으면 @ Mike_G가 앱과 동일한 디렉토리에 파일을 두는 것이 좋습니다.

EDIT : 귀하의 의견을 참고하면, C:\Program Files\...은 쓰기 불가능합니다. 원하는 경우를 제외하고 모든 경우에 사용자 별 (좋은 기본값으로) 사용자 지정을 고려할 것입니다. 한 곳에서 통합되었습니다. 이 경우 앱의 구성에 포함시켜 사용자가 데이터를 저장할 위치를 결정하도록 할 수 있습니다.

+0

좋아요, 나는 @stevemegson의 답을 더 좋아합니다. ;) (기한 만료일과 그 모든 것) – JMD

0

데이터가 모든 사용자를위한 것이라면 "공유 문서"영역을 사용해보십시오. XP에서는 C : \ Documents and Settings \ All Users \ Documents에 있습니다. Vista에 대해서는 잘 모르겠지만 C : \ Users에있을 가능성이 큽니다.

4

Environment.GetFolderPath는 Windows에서 저장하려는 위치를 알려줍니다.

사용자 별 데이터의 경우

,

Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData)); 

는 일반적으로 C 반환 : \ 사용자 \

공통 데이터의 경우

로밍 % 사용자 % \의 AppData \를

Environment.GetFolderPath(Environment.SpecialFolder.CommonApplicationData)); 

는 일반적으로 C 반환 \프로그램 데이터.

이렇게하면 이전 버전의 Windows에서 올바른 작업을 수행 할 수 있습니다. XP는 일반적으로 C : \ Documents and Settings \ % user % \ Application Data와 C : \ Documents and Settings \ All Users \ Application Data를 반환합니다.

+0

이 답변에 대한 두 가지주의 사항 : - C : \ ProgramData는 일반 사용자의 경우 기본적으로 읽기 전용이며, - CommonApplicationData와 ApplicationData는 모두 문서를 나타내는 파일을위한 것이 아닙니다. –

+0

실제로 ApplicationData는 사용자가 볼 수 없습니다. Environment.SpecialFolder.MyDocuments 하위 폴더 아래에 문서처럼 또는 프로젝트에 저장하십시오. –

+0

@wcoenen ApplicationData는 사용자에게 표시됩니다. 그것은 단지 발견되지 않습니다. :-) –

1

.Net Framework에서 IsolatedStorage을 사용해보세요. 그것은 당신을 위해이 일을 할 것입니다.

프레임 워크는 드라이브, 폴더 및 파일 등을 관리하는 대신 이러한 위치를 관리 할 수 ​​있습니다. 목적은 사용자 권한에 대해 걱정할 필요가없는 영역을 갖는 것입니다.

아래의 코드 시퀀스는 MSDN에서 곧바로 나오지만이 파일을 사용하는 방법을 정확하게 보여줍니다.


using System; 
using System.IO; 
using System.IO.IsolatedStorage; 

public class ReadingAndWritingToFiles{ 

    public static int Main(){ 

     // Get an isolated store for this assembly and put it into an 
     // IsolatedStoreFile object. 

     IsolatedStorageFile isoStore = IsolatedStorageFile.GetStore(IsolatedStorageScope.User | IsolatedStorageScope.Assembly, null, null); 

     // This code checks to see if the file already exists. 

     string[] fileNames = isoStore.GetFileNames("TestStore.txt"); 
     foreach (string file in fileNames){ 
     if(file == "TestStore.txt"){ 

      Console.WriteLine("The file already exists!"); 
      Console.WriteLine("Type \"StoreAdm /REMOVE\" at the command line to delete all Isolated Storage for this user."); 

      // Exit the program. 

      return 0; 
     } 
     } 

     writeToFile(isoStore); 

     Console.WriteLine("The file \"TestStore.txt\" contains:"); 
     // Call the readFromFile and write the returned string to the 
     //console. 

     Console.WriteLine(readFromFile(isoStore)); 

     // Exit the program. 

     return 0; 

    }// End of main. 


    // This method writes "Hello Isolated Storage" to the file. 

    private static void writeToFile(IsolatedStorageFile isoStore){ 

     // Declare a new StreamWriter. 

     StreamWriter writer = null; 

     // Assign the writer to the store and the file TestStore. 

     writer = new StreamWriter(new IsolatedStorageFileStream("TestStore.txt", FileMode.CreateNew,isoStore)); 

     // Have the writer write "Hello Isolated Storage" to the store. 

     writer.WriteLine("Hello Isolated Storage"); 

     writer.Close(); 

     Console.WriteLine("You have written to the file."); 

    }// End of writeToFile. 


    // This method reads the first line in the "TestStore.txt" file. 

    public static String readFromFile(IsolatedStorageFile isoStore){ 

     // This code opens the TestStore.txt file and reads the string. 

     StreamReader reader = new StreamReader(new IsolatedStorageFileStream("TestStore.txt", FileMode.Open,isoStore)); 

     // Read a line from the file and add it to sb. 

     String sb = reader.ReadLine(); 

     // Close the reader. 

     reader.Close(); 

     // Return the string. 

     return sb.ToString(); 

    }// End of readFromFile. 
} 
+0

다른 종류의 격리에 대한 흥미로운, 특히 읽기 : http://msdn.microsoft.com/en-us/library/eh5d60e1.aspx 그러나 그것은 어셈블리의 강력한 이름 당 격리를 강요하는 것처럼 보입니다. 향후 버전의 데이터 –

관련 문제