简体   繁体   English

为什么当使用计数器给文件名编号时,它给了它奇怪的数字?

[英]Why when giving files names numbers to the names using a counter it's giving it strange numbers?

The first saved file name on the hard disk is: 201701311645---0 then 201701311645---1 then 201701311645---20 then 201701311645---21 then 201701311645---40 and 201701311645---41硬盘上第一个保存的文件名是: 201701311645---0 然后201701311645---1 然后201701311645---20 然后201701311645---21 然后201701311645---70 和1311---10

But i want it to be saved as: 201701311645---0 then 201701311645---1 then 201701311645---2 then 201701311645---3 then 201701311645---4 and 201701311645---5但我希望它被保存为: 201701311645---0 然后 201701311645---1 然后 201701311645---2 然后 201701311645---3 然后 201701311645---4 和 10164515---

In the top i added a counter variable在顶部我添加了一个计数器变量

private int countFilesNames = 0;

Then in a dowork event i also reset the counter to 0 once so if i start the backgroundworker over again it will start from 0.然后在 dowork 事件中,我还将计数器重置为 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;
                }
            }
        }

Then in the downloadFile method然后在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;
        }

I build the file name:我构建文件名:

writer = new FileStream(this.LocalDirectory + "\\" + file.Name +
                    "---" + countFilesNames + ".png", System.IO.FileMode.Create);

The move the counter forward by 1:将计数器向前移动 1:

countFilesNames += 1;

But i'm getting other files names then i wanted.但是我得到了我想要的其他文件名。

Maybe there is a better way to give the files names some identity ?也许有更好的方法给文件名一些身份? The problem is that if i will not give the files names some identity it will overwrite the files all the time.问题是,如果我不给文件名一些身份,它会一直覆盖文件。 The files names are the same the content is not so i need to give each file another name.文件名相同,内容不同,所以我需要给每个文件另一个名字。

Why don't you just increment the counter only when a file is written (since the variable doesn't look like it is accessed elsewhere) and not below:为什么不只在写入文件时才增加计数器(因为变量看起来不像在其他地方被访问)而不是在下面:

writer = new FileStream(this.LocalDirectory + "\\" + file.Name +
                "---" + countFilesNames++ + ".png", System.IO.FileMode.Create);

This way the counter won't be incremented on errors.这样计数器就不会因错误而增加。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM