2014-12-06 5 views
0

간단하게 들리지만 그렇지 않습니다. 다른 디렉토리 (c:\Users\Public\Folder)에 기본 디렉토리 (..\bin\debug\2013-10-5-05-06.txt)에서, 2013-10-5-05-06.txt :처럼 보일 것입니다파일을 기본 디렉토리에서 다른 디렉토리로 이동하는 방법은 무엇입니까?

string newFileName = string.Format("{0}-{1}-{2}-t{3:00}-{4:00}.txt", 2013, 10, 5, 05, 06); 

: 나는이처럼 만든 파일을 이동하려합니다. 거의 같은 이름 (작은 차이)이있는 다른 파일이 같은 폴더로 이동되도록 파일의 이름을 유지하려고합니다. 나는 여러 가지 방법을 시도 (Path.Combine(), string.Concat()..) 성공하지 않고.

+0

당신의 질문은 파일을 이동하는 대신 경로를 만드는 방법에 관한 것 같습니다. – t3chb0t

답변

0

그냥

string CurrentFileNameAndPath; //the path the file you want to move 
string newPath; //only the new the folderPath 
System.IO.FileInfo FileYouWantToMove = new System.IO.FileInfo(CurrentFileNameAndPath); 
string NewFileNameAndPath = newPath + "\\" + FileYouWantToMove.Name; //remember that using fullname will get the folder and filename 
FileYouWantToMove.MoveTo(NewFileNameAndPath); 

그래서 내가이 파일 C를 예로 사용할 수 있습니다이 조각 사용 /Dir1/file1.txt을 나는 C에 자사의 디렉토리를 변경하려면 :/Dir2라는/오른쪽? 다음은이

string CurrentFileNameAndPath = @"C:/Dir1/file1.txt"; 
string newPath = @"C:/Dir2/"; 
System.IO.FileInfo FileYouWantToMove = new System.IO.FileInfo(CurrentFileNameAndPath); 
string NewFileNameAndPath = newPath + "\\" + FileYouWantToMove.Name; 
FileYouWantToMove.MoveTo(NewFileNameAndPath); 

결과처럼 될 것입니다 것입니다 C에서 해당 파일 : /Dir1/file1.txt가 C에 지금있을 것입니다 : 그것은 이동과 같은 파일 이름을 maintened 된 /Dir2/file1.txt 및 확장

+0

이것은 다음과 같은 의미입니다. string CurrentFileNameAndPath = Path.GetFullPath (newFileName); – Steve

+0

예 C : /Users/YourName/Desktop/File.txt와 같이 변경하려는 파일의 currentFullName이어야합니다. – Patrick

+0

FileYouWantToMove는 newFileName 또는 2013-10-5-05-06.txt입니까? 나는 혼란 스럽다. newFileName.Name 및 newFileName.MoveTo()는 할 수 없습니다. – Steve

0

이런 식으로 뭔가가

var srcFile = "..\bin\debug\2013-10-5-05-06.txt"; 
var destFolder = Path.GetDirectoryName(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory)); 
var destFile = Path.Combine(destFolder, Path.GetFileName(srcFile)); 
File.Move(srcFile, destFile); 

그냥 Move 다양한 예외 예를 던질 수 있음을 염두에 두어야 실제로 매우 간단하다 IOException/UnauthorizedAccessException 등으로 적절하게 처리해야합니다.

+0

Path.GetDirectoryPath : 경로 아래에 GetDirectoryPath가 없습니까? – Steve

+0

@Steve는 오타였습니다. 이미 답변을 편집했습니다. – James

+0

그것은 작동하지 않습니다. 내가 문자열을 시도 srcFile = Path.GetFullPath (newFileName); 나머지 코드를 사용했지만 아무 일도 일어나지 않았습니다. 방금 파일이 있습니다. – Steve

관련 문제