الفريق العربي للبرمجةأرشيف المنتديات · 2000 – 2023
نسخة أرشيفية للقراءة فقط — التسجيل والمشاركة مغلقان، والمحتوى محفوظ كما كان.

ما الخطأ هنا؟ قراءة وكتابة ملفات

بدأه عزوز الحسني في 30 أبريل 2011 · 4 رد · 296 مشاهدة · في Microsoft Visual C#.NET
مشاركة: واتساب X فيسبوك تيليجرام
#1 صاحب الموضوع

السلام عليكم

انا جديد على بيئة .Net في الحقيقة كنت كاره لها.

عند تنفيذ هذا الكود يظهر أنه يقرأ أول 1024 بايت ولا يتمكن من متابعة القراءة، ما الحل؟

        private void button5_Click(object sender, EventArgs e)
        {
            int remaining, offset, offset2, read, sizeOfAll = 0;
            byte[] buffer = new byte[1024];

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (string filename in listBox1.Items)
                {
                    FileInfo f = new FileInfo(filename);
                    sizeOfAll += (int) f.Length;
                }
                progressBar1.Maximum = sizeOfAll;
                progressBar1.Value = 0;
                offset2 = 0;

                Stream fw = File.Create(saveFileDialog1.FileName);

                foreach (string filename in listBox1.Items)
                {
                    Stream fr = File.OpenRead(filename);
                    FileInfo f = new FileInfo(filename);
                    offset = 0;
                    remaining = (int) f.Length;
                    while (remaining > 0) {
                        read = fr.Read(buffer, offset, 1024);
                        fw.Write(buffer, offset2, read);
                        remaining -= read;
                        offset += read;
                        offset2 += read;
                        progressBar1.Value += read;
                    }
                    fr.Close();
                }
                fw.Close();
            }
        }
#2

السلام عليكم ورحمه الله وبركاته

اخي حاول استبدال ال While بتاعتك ب


{
read = fr.Read(buffer, offset, 1024);
if(read<=0) break;
fw.Write(buffer, offset2, read);
progressBar1.Value += read;
}
while(true)

.:: Wish to become better and better ::.

#3

أشكرك على الرد، لم يتغير شيء.

رسالة الخطأ:

Offset and length were out of bounds for the array or count is greater than the number of elements from index to the end of the source collection.

#4

بعد البحث عرفت الآتي:

The documentation is a bit confusing. The "offset" is the offset in the array that you supply, not the file you're reading.

الحل بعد التعديل:

        private void button5_Click(object sender, EventArgs e)
        {
            int read, sizeOfAll = 0;
            byte[] buffer;

            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                foreach (string filename in listBox1.Items)
                {
                    FileInfo f = new FileInfo(filename);
                    sizeOfAll += (int) f.Length;
                }
                progressBar1.Maximum = sizeOfAll;
                progressBar1.Value = 0;
                Stream fw = File.Create(saveFileDialog1.FileName);
                foreach (string filename in listBox1.Items)
                {
                    Stream fr = File.OpenRead(filename);
                    FileInfo f = new FileInfo(filename);
                    while (true) {
                        buffer = new byte[1024];
                        read = fr.Read(buffer, 0, 1024);
                        fw.Write(buffer, 0, read);
                        if (read == 0)
                        {
                            break;
                        }
                        progressBar1.Value += read;
                    }
                    fr.Close();
                }
                fw.Close();
            }
        }
#5

السلام عليكم ورحمه الله وبركاته

اخي كما ذكرت لك سابقا ... اعتمدت على القيمه الافتراضيه لل Offset وهي 0 وقمت بحذف الاسطر التي تقوم بتغييرها يمكن تكون مافهمت قصدي؟!!

.:: Wish to become better and better ::.

مواضيع مشابهة