Program Tip

Path.GetTempPath ()의 반환 값을 결정하는 것은 무엇입니까?

programtip 2020. 11. 15. 11:42
반응형

Path.GetTempPath ()의 반환 값을 결정하는 것은 무엇입니까?


현재는 Path.GetTempPath()로그 파일을 작성할 위치를 파악하는 데 사용 하고 있지만 최근에는 반환 된 경로가 예상과 다른 사용자 컴퓨터를 발견했습니다.

일반적으로 반환 된 경로는 C : \ Documents and Settings \ [userid] \ Local Settings \ Temp 이지만이 경우에는 C : \ Temp입니다.

이것은 일반적으로 문제가되지 않지만 어떤 이유로 문제의 사용자는 C : \ Temp에 쓸 수있는 권한이 없었습니다 .

환경 변수를 두 번 확인했고 USER 환경 변수는 예상대로 C : \ Documents and Settings \ [userid] \ Local Settings \ Temp 를 가리키고 SYSTEM 환경 변수는 C : \ WINNT \ Temp를 가리 킵니다 .

그래서 ... 어디 Path.GetTempPath()에서 가치를 얻습니까? 그룹 정책? 기재?

Google 검색을했지만 아무 소용이 없습니다.


(Reflector 사용) Path.GetTempPath()궁극적으로 Win32 함수 GetTempPath (kernel32.dll에서)를 호출합니다 . 이 상태에 대한 MDSN 문서 :

GetTempPath 함수는 다음 순서로 환경 변수의 존재를 확인하고 발견 된 첫 번째 경로를 사용합니다.

  • TMP 환경 변수로 지정된 경로입니다.
  • TEMP 환경 변수로 지정된 경로입니다.
  • USERPROFILE 환경 변수로 지정된 경로입니다.
  • Windows 디렉토리.

또한 경로가 실제로 존재하는지 또는에 쓸 수 있는지 여부를 확인하지 않는다고 명시 하므로 존재하지 않는 경로 또는 액세스 할 수없는 경로에 로그 파일을 쓰려고 할 수 있습니다. .


면책 조항 : 대답은 아니지만 중요한 읽기!

단일 디렉토리에서 65536에 도달하면 프레임 워크가 더 이상 생성하지 않고 앱이 폭발하므로 임시 파일을 정리해야한다는 사실을 인식하는 것이 매우 중요합니다!

수개월 및 수개월에 걸쳐 누적되며 다음과 같은 메시지가 표시됩니다.

System.IO.IOException: The file exists.

  at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
  at System.IO.__Error.WinIOError()
  at System.IO.Path.InternalGetTempFileName(Boolean checkHost)
  at System.IO.Path.GetTempFileName():

TFS는 빌드를 시도 할 때 다음을 제공합니다.

TF215097: An error occurred while initializing a build for build 
definition XXXXX: The file exists. 

당신이해야 할 일은 Path.GetTempPath()폴더를 찾아서del tmp*

참고 : 임시 파일을 만드는 ASP.NET 응용 프로그램이있는 경우 해당 임시 디렉터리는 현재 로그인 한 사용자와 다를 수 있습니다.

의심 스럽거나 패닉 상태 인 경우 사용중인 위치를 인쇄하기 위해 aspx 페이지를 만듭니다.

 TempPath.aspx
 <%@ Page Language="C#"%>
 Temp path: <%= System.IO.Path.GetTempPath() %>

NetworkService내가 얻을 때 달리는 나를 위해

 C:\Windows\TEMP\

AppPool (www.example.com이라는 이름)으로 실행할 때 경로는 다음과 같을 수 있습니다.

 C:\Users\www.example.com\AppData\Local\Temp

추신. 나중에 파일 이름이 증가하여 파일을 삭제하더라도 이런 일이 발생할 수 있다고 생각합니다.


콘솔 응용 프로그램 인 경우 GetTempPath ()가 로컬 사용자의 Documents & Settings \ user \ Local Settings \ Temp 경로를 다시 가져올 수 있으며, C : \ WINDOWS \ Temp (서버에서)를 다시 가져올 수 있음을 확인했습니다. 클라이언트에서 실행되는 웹 앱. 전자의 경우 큰 문제가 아닙니다. 앱을 실행하는 계정은 해당 폴더에 대한 권한을 갖습니다. 후자의 경우 앱 풀 ID 계정 (또는 웹 앱의 Web.config 파일에서 가장하는 데 사용할 수있는 계정)에 C : \ WINDOWS \ Temp에 대한 권한이없는 경우 큰 문제 일 수 있습니다. 서버 (그렇지 않은 큰 기회). 따라서 내 콘솔 앱의 경우 임시 파일이 작성되는 위치에 의문의 여지가 없습니다. INI 파일에 문자열을 하드 코딩하는 것이 저에게 가장 좋고 쉬운 방법이며 웹 앱의 경우 웹에서 하드 코딩하는 것이 좋습니다.

public static string findFileDirectory(string file)
{
    // Get the directory where our service is being run from
    string temppath = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location);
    // Ensure proper path notation so we can add the INI file name
    if (!temppath.EndsWith(@"\")) temppath += @"\";

    return temppath;
}

다음과 같이 호출하십시오.

    string tempFolderPath = findFileDirectory("Web.config");
    tempFolderPath = tempFolderPath.Replace(@"\\", @"\");

이전에 Path.GetTempPath ()를 사용한 곳 대신 "tempFolderPath"를 사용하십시오. 이 함수는 멋지게 작동하며이 악한 GetTempPath () 메서드 대신 내 코드에서 사용하므로 ASP Temp Files 폴더에 작업에 필요한 모든 권한이 있어야하기 때문에 내 앱이 필요한 작업을 수행 할 수 있다는 것을 알고 있습니다. DOMAIN \ NETWORK SERVICE 및 앱 풀 ID 계정에는 모든 권한이 필요합니다. tempFolderPath는 후행 슬래시로 끝나므로 올바른 경로를 얻으려면 변수 / 파일 이름과 직접 연결하십시오.

-톰

추신 :이 함수가 작동하도록하려면 System.IO 및 System.Reflection이라는 두 개의 네임 스페이스를 추가해야합니다.


GetTempPath 함수를 호출합니다 . 문서는 확인하는 환경 변수를 설명합니다.


다음을 사용하여 데이터에 적합한 위치를 결정하십시오.

Environment.GetFolderPath(Environment.SpecialFolder folder);

어디 Specialfolder

// Summary:
//     Specifies enumerated constants used to retrieve directory paths to system
//     special folders.
[ComVisible(true)]
public enum SpecialFolder
{
  // Summary:
  //     The logical Desktop rather than the physical file system location.
  Desktop = 0,
  //
  // Summary:
  //     The directory that contains the user's program groups.
  Programs = 2,
  //
  // Summary:
  //     The directory that serves as a common repository for documents.
  Personal = 5,
  //
  // Summary:
  //     The "My Documents" folder.
  MyDocuments = 5,
  //
  // Summary:
  //     The directory that serves as a common repository for the user's favorite
  //     items.
  Favorites = 6,
  //
  // Summary:
  //     The directory that corresponds to the user's Startup program group.
  Startup = 7,
  //
  // Summary:
  //     The directory that contains the user's most recently used documents.
  Recent = 8,
  //
  // Summary:
  //     The directory that contains the Send To menu items.
  SendTo = 9,
  //
  // Summary:
  //     The directory that contains the Start menu items.
  StartMenu = 11,
  //
  // Summary:
  //     The "My Music" folder.
  MyMusic = 13,
  //
  // Summary:
  //     The directory used to physically store file objects on the desktop.
  DesktopDirectory = 16,
  //
  // Summary:
  //     The "My Computer" folder.
  MyComputer = 17,
  //
  // Summary:
  //     The directory that serves as a common repository for document templates.
  Templates = 21,
  //
  // Summary:
  //     The directory that serves as a common repository for application-specific
  //     data for the current roaming user.
  ApplicationData = 26,
  //
  // Summary:
  //     The directory that serves as a common repository for application-specific
  //     data that is used by the current, non-roaming user.
  LocalApplicationData = 28,
  //
  // Summary:
  //     The directory that serves as a common repository for temporary Internet files.
  InternetCache = 32,
  //
  // Summary:
  //     The directory that serves as a common repository for Internet cookies.
  Cookies = 33,
  //
  // Summary:
  //     The directory that serves as a common repository for Internet history items.
  History = 34,
  //
  // Summary:
  //     The directory that serves as a common repository for application-specific
  //     data that is used by all users.
  CommonApplicationData = 35,
  //
  // Summary:
  //     The System directory.
  System = 37,
  //
  // Summary:
  //     The program files directory.
  ProgramFiles = 38,
  //
  // Summary:
  //     The "My Pictures" folder.
  MyPictures = 39,
  //
  // Summary:
  //     The directory for components that are shared across applications.
  CommonProgramFiles = 43,
}

If you are using C# on MacOS using Mono Framework then value returned by Path.GetTempPath() is value of environment variable TMPDIR.

Running echo $TMPDIR usually returns value like :

/var/folders/{2 character random-string}/{random-string}/T

참고URL : https://stackoverflow.com/questions/2365307/what-determines-the-return-value-of-path-gettemppath

반응형