2010-04-28 3 views
19

파일을이 두 가지 방법으로 이동하는 데 차이가 있습니까?FileInfo.MoveTo()와 File.Move()

System.IO.FileInfo f = new System.IO.FileInfo(@"c:\foo.txt"); 
f.MoveTo(@"c:\bar.txt"); 

//vs 

System.IO.File.Move(@"c:\foo.txt", @"c:\bar.txt"); 
+1

무엇이 문제입니까? 동일한 API를 사용하기위한 두 가지 방법 인 것으로 보입니다. 루프 된 성능 또는 모범 사례를 찾고 있습니까, 아니면 ??? – jcolebrand

+0

정확히 같은 일을하는 것처럼 보이는 두 가지 방법이 왜 궁금했습니다. –

+0

잘하면 아래 스 니펫은 어떻게 다른지 보여줍니다. 또한 레드 게이트에서 (현재 무료) 리플 렉터 제품을 사용하고 있지 않다면, 당신은해야합니다. – jcolebrand

답변

18

이 MSDN 페이지 http://msdn.microsoft.com/en-us/library/akth6b1k.aspx "주의"섹션에서보세요 :

당신이 객체를 여러 번 재사용 에서는 FileInfo 대신에 해당하는 정적 메소드의 인스턴스 메소드를 사용하는 것을 고려하기 위하여려고하는 경우 클래스의 보안 검사가 항상 필요한 것은 아니기 때문에

이 차이점은 파일 (디렉토리)과 FileInfo (디렉토리 정보) 클래스간에 가장 중요합니다.

UPD : 비슷한 질문 https://stackoverflow.com/a/1324808/380123

12

통해 레드 게이트 리플렉터 :

File.Move()

public static void Move(string sourceFileName, string destFileName) 
{ 
    if ((sourceFileName == null) || (destFileName == null)) 
    { 
     throw new ArgumentNullException((sourceFileName == null) ? "sourceFileName" : "destFileName", Environment.GetResourceString("ArgumentNull_FileName")); 
    } 
    if ((sourceFileName.Length == 0) || (destFileName.Length == 0)) 
    { 
     throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), (sourceFileName.Length == 0) ? "sourceFileName" : "destFileName"); 
    } 
    string fullPathInternal = Path.GetFullPathInternal(sourceFileName); 
    new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new string[] { fullPathInternal }, false, false).Demand(); 
    string dst = Path.GetFullPathInternal(destFileName); 
    new FileIOPermission(FileIOPermissionAccess.Write, new string[] { dst }, false, false).Demand(); 
    if (!InternalExists(fullPathInternal)) 
    { 
     __Error.WinIOError(2, fullPathInternal); 
    } 
    if (!Win32Native.MoveFile(fullPathInternal, dst)) 
    { 
     __Error.WinIOError(); 
    } 
} 

및 FileInfo.MoveTo()

public void MoveTo(string destFileName) 
{ 
    if (destFileName == null) 
    { 
     throw new ArgumentNullException("destFileName"); 
    } 
    if (destFileName.Length == 0) 
    { 
     throw new ArgumentException(Environment.GetResourceString("Argument_EmptyFileName"), "destFileName"); 
    } 
    new FileIOPermission(FileIOPermissionAccess.Write | FileIOPermissionAccess.Read, new string[] { base.FullPath }, false, false).Demand(); 
    string fullPathInternal = Path.GetFullPathInternal(destFileName); 
    new FileIOPermission(FileIOPermissionAccess.Write, new string[] { fullPathInternal }, false, false).Demand(); 
    if (!Win32Native.MoveFile(base.FullPath, fullPathInternal)) 
    { 
     __Error.WinIOError(); 
    } 
    base.FullPath = fullPathInternal; 
    base.OriginalPath = destFileName; 
    this._name = Path.GetFileName(fullPathInternal); 
    base._dataInitialised = -1; 
} 
3

내가 볼 수있는 유일한 유의 한 차이가 File.Move이입니다 정적이고 FileInfo.MoveTo은 아닙니다.
그 외에도 그들은 거의 동일한 코드를 실행합니다.

+0

예, 동의합니다. FileInfo는 FileSystemInfo를 상속하고 File은 Object를 상속합니다. FileSystemInfo는 Marshalling 만 제공하므로 FileInfo는 Managed Friendly가 될 것입니다. – jcolebrand

5

중요한 차이 같은 설명은 FileInfo.MoveTo()을 대상 경로로에서는 FileInfo 개체의 파일 경로를 업데이트 할 것입니다. File.Move()는 문자열을 입력으로 사용하기 때문에 분명히 아닙니다.

관련 문제