form1에는 하나의 파일 또는 여러 파일의 디렉토리에서 파일을 선택할 수있는 두 개의 버튼이 있습니다. 두 번째 단추는 선택한 디렉터리의 모든 파일을 가져 오기 위해 디렉터리에서 파일을 선택하는 것입니다.FileInfo []가 필요한 경우 어떻게 사용할 수 있습니까? 아니면 필요하면 문자열 []을 사용할 수 있습니까?
지금 내가 파일을 업로드하기 위해 사용하고 클래스가/내 FTP로 디렉토리를 다음
public static DirectoryInfo d;
public static string[] files;
private FileInfo[] dirflist;
난 경우에 그것을 사용하고 있습니다 : 내가 한 클래스의 상단에 :
private void FtpProgress_DoWork(object sender, DoWorkEventArgs e)
{
try
{
dirflist = d.GetFiles();
//if (dirflist.Length > 0)
//{
foreach (string txf in files)
{
string fn = txf;//txf.Name;
BackgroundWorker bw = sender as BackgroundWorker;
f = e.Argument as FtpSettings;
string UploadPath = String.Format("{0}/{1}{2}", f.Host, f.TargetFolder == "" ? "" : f.TargetFolder + "/", Path.GetFileName(fn));//f.SourceFile));
if (!UploadPath.ToLower().StartsWith("ftp://"))
UploadPath = "ftp://" + UploadPath;
FtpWebRequest request = (FtpWebRequest)WebRequest.Create(UploadPath);
request.UseBinary = true;
request.UsePassive = f.Passive;
request.Method = WebRequestMethods.Ftp.UploadFile;
request.Timeout = 300000;
request.Credentials = new NetworkCredential(f.Username, f.Password);
long FileSize = new FileInfo(f.SourceFile).Length;
string FileSizeDescription = GetFileSize(FileSize);
int ChunkSize = 4096, NumRetries = 0, MaxRetries = 50;
long SentBytes = 0;
byte[] Buffer = new byte[ChunkSize];
using (Stream requestStream = request.GetRequestStream())
{
using (FileStream fs = File.Open(f.SourceFile, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
{
int BytesRead = fs.Read(Buffer, 0, ChunkSize);
while (BytesRead > 0)
{
try
{
if (bw.CancellationPending)
return;
requestStream.Write(Buffer, 0, BytesRead);
SentBytes += BytesRead;
string SummaryText = String.Format("Transferred {0}/{1}", GetFileSize(SentBytes), FileSizeDescription);
bw.ReportProgress((int)(((decimal)SentBytes/(decimal)FileSize) * 100), SummaryText);
}
catch (Exception ex)
{
Debug.WriteLine("Exception: " + ex.ToString());
if (NumRetries++ < MaxRetries)
{
fs.Position -= BytesRead;
}
else
{
throw new Exception(String.Format("Error occurred during upload, too many retries. \n{0}", ex.ToString()));
}
}
BytesRead = fs.Read(Buffer, 0, ChunkSize);
}
}
}
using (FtpWebResponse response = (FtpWebResponse)request.GetResponse())
System.Diagnostics.Debug.WriteLine(String.Format("Upload File Complete, status {0}", response.StatusDescription));
}
//}
}
catch (WebException ex)
{
switch (ex.Status)
{
case WebExceptionStatus.NameResolutionFailure:
ConnectionError = "Error: Please check the ftp address";
break;
case WebExceptionStatus.Timeout:
ConnectionError = "Error: Timout Request";
break;
}
}
}
이제 문자열 [] 배열을 반복합니다. 여러 파일을 선택하고 있기 때문에.
그러나 내가 디렉토리를 선택할 수 있습니다. 그리고 난의 DirectoryInfo (D 변수) 등을 사용할 필요가있을에서는 FileInfo []
내가 사용하는 경우에서는 FileInfo [] 다음은 같다 :
dirflist = d.GetFiles();
if (dirflist.Length > 0)
{
foreach (FileInfo txf in dirfilist)
{
string fn = txf.Name;
하지만 난 싶지 않아 string [] 또는 FileInfo []에 대해서만 다시 모든 코드를 복사하십시오. foreach에서 foreach 또는 string []을 사용하여 FileInfo []를 사용할 수있는 무언가를 만들고 싶습니다.
그리고 때로는 여러 파일을 업로드하고 내부의 모든 파일을 포함하는 디렉토리를 업로드 할 때도 있습니다.
그래서 전체 코드를 복제하고 한 번 [] 문자열을 사용하고 FileInfo []를 사용하는 것이 더 좋을까요?
지금private void CopyFile(string fn)
{
BackgroundWorker bw = sender as BackgroundWorker;
f = e.Argument as FtpSettings;
...
}
당신이 원하는 처리 여부를 결정 : 나는 두 가지 방법을 의미 하나
그냥'd.GetFileSystemInfos()'를 사용하십시오. 질문하기 전에 살펴 보았습니까? – BrokenGlass
Ftp 전송 프로세스를 읽었습니다. 함수의 작업을 나눌 것입니다. While는 CancellationPending 처리를 포함하는 Function (파일 처리시 1을 반환하고 BW 취소시 0을 반환하고 Exception의 경우 -1을 반환 함)에서 파일 경로 당 파일 정보 (또는 FileInfo)를 가져 오는 작업을 이동합니다. 필요한 모든 멤버가있는 사용자 정의 클래스를 반환하는 함수 (버퍼링을 처리 할 함수에 해당 클래스 전달) - UI 용도로, 이러한 클래스에는 버퍼링, FTP 전송 및 발생 이벤트 처리 : chunck send, transfer complete , 취소 된... –