ハードディスクの最初の保存ファイル名:201701311645 --- 0次に201701311645 --- 1次に201701311645 --- 20次に201701311645 --- 21次に201701311645 --- 40および201701311645 --- 41ファイル名を与えるときに、名前を数字で指定すると、それに奇妙な数字が与えられます。
しかし、私はそれはとして保存することにしたい。そして、201701311645 --- 0その後、201701311645 --- 1その後、201701311645 --- 201701311645その後、2 --- 3 201701311645 --- 4と201701311645 --- 5
トップでは、私は再びそれがSTAます上のBackgroundWorkerを起動するので、もし私も一度カウンターを0にリセットするdoworkイベントでカウンタ変数その後
private int countFilesNames = 0;
を追加しました0
private void bgwDownloader_DoWork(object sender, DoWorkEventArgs e)
{
Int32 fileNr = 0;
countFilesNames = 0;
if (this.SupportsProgress) { calculateFilesSize(); }
if (!Directory.Exists(this.LocalDirectory)) { Directory.CreateDirectory(this.LocalDirectory); }
while (fileNr < this.Files.Count && !bgwDownloader.CancellationPending)
{
m_fileNr = fileNr;
downloadFile(fileNr);
if (bgwDownloader.CancellationPending)
{
fireEventFromBgw(Event.DeletingFilesAfterCancel);
cleanUpFiles(this.DeleteCompletedFilesAfterCancel ? 0 : m_fileNr, this.DeleteCompletedFilesAfterCancel ? m_fileNr + 1 : 1);
}
else
{
fileNr += 1;
}
}
}
からRTするとdownloadFile方法
private void downloadFile(Int32 fileNr)
{
FileStream writer = null;
m_currentFileSize = 0;
fireEventFromBgw(Event.FileDownloadAttempting);
FileInfo file = this.Files[fileNr];
Int64 size = 0;
Byte[] readBytes = new Byte[this.PackageSize];
Int32 currentPackageSize;
System.Diagnostics.Stopwatch speedTimer = new System.Diagnostics.Stopwatch();
Int32 readings = 0;
Exception exc = null;
try
{
writer = new FileStream(this.LocalDirectory + "\\" + file.Name +
"---" + countFilesNames + ".png", System.IO.FileMode.Create);
}
catch(Exception err)
{
string ggg = err.ToString();
}
HttpWebRequest webReq;
HttpWebResponse webResp = null;
try
{
webReq = (HttpWebRequest)System.Net.WebRequest.Create(this.Files[fileNr].Path);
webResp = (HttpWebResponse)webReq.GetResponse();
size = webResp.ContentLength;
}
catch (Exception ex) { exc = ex; }
m_currentFileSize = size;
fireEventFromBgw(Event.FileDownloadStarted);
if (exc != null)
{
bgwDownloader.ReportProgress((Int32)InvokeType.FileDownloadFailedRaiser, exc);
}
else
{
m_currentFileProgress = 0;
while (m_currentFileProgress < size && !bgwDownloader.CancellationPending)
{
while (this.IsPaused) { System.Threading.Thread.Sleep(100); }
speedTimer.Start();
currentPackageSize = webResp.GetResponseStream().Read(readBytes, 0, this.PackageSize);
m_currentFileProgress += currentPackageSize;
m_totalProgress += currentPackageSize;
fireEventFromBgw(Event.ProgressChanged);
writer.Write(readBytes, 0, currentPackageSize);
readings += 1;
if (readings >= this.StopWatchCyclesAmount)
{
m_currentSpeed = (Int32)(this.PackageSize * StopWatchCyclesAmount * 1000/(speedTimer.ElapsedMilliseconds + 1));
speedTimer.Reset();
readings = 0;
}
}
speedTimer.Stop();
writer.Close();
webResp.Close();
if (!bgwDownloader.CancellationPending) { fireEventFromBgw(Event.FileDownloadSucceeded); }
}
fireEventFromBgw(Event.FileDownloadStopped);
countFilesNames += 1;
}
に私は、ファイル名を構築する:
writer = new FileStream(this.LocalDirectory + "\\" + file.Name +
"---" + countFilesNames + ".png", System.IO.FileMode.Create);
1によって前方にカウンターを移動:
countFilesNames += 1;
しかし、私は他のファイル名を取得しているnted。
多分、ファイル名にいくつかのIDを付けるより良い方法がありますか?問題は、ファイル名に何らかの識別情報を付けないと、ファイルを常に上書きするということです。ファイル名は同じですので、各ファイルに別の名前を付ける必要はありません。
「デバッガを使用してコードを実行する」から始めて、このコードをリファクタリングし、機能をより管理しやすい/読みやすいコードブロック/メソッドに分割します。これは非常に複雑です。 – MethodMan
Areファイルの辞書順に不平を言うだけですか? Windowsでは常に201701311645 --- 11.png before 201701311645と表示されます。2.png注文を変更したい場合は0を挿入して、201701311645 --- 02.png、201701311645 --- 11.pngです。 –