Welcome guest. Before posting on our computer help forum, you must register. Click here it's easy and free.

Author Topic: C# Copy file problem.  (Read 8585 times)

0 Members and 1 Guest are viewing this topic.

hibyy

    Topic Starter


    Rookie

    • Experience: Familiar
    • OS: Windows 8
    C# Copy file problem.
    « on: July 24, 2012, 08:16:15 AM »
    Hello, I am having some problems with a code that I am working on it is supposed to copy everything in one folder and then place the copy where the file is.

    The Problem is when I test it, it incorrectly creates the exe files and they end up beeping at me several times before closing when I run the copies.

    is there a way I could improve this code or is it not possible to copy exe files? Every other file type I have tried worked with this method.

    Code: [Select]
    using System;
    using System.IO;
    namespace CopyFiles
    {
    public class Copy
    {
    public static void Main()
    {
    int Byte;
    string Lines;
    Byte = 0;
    Lines = "";
    string [] files = Directory.GetFiles("TestFolder");
    foreach(string file in files)
    {
    Lines = "";
    string filee = file.Replace("TestFolder\\", "");
    FileStream A = new FileStream(@file, FileMode.Open);
    for (int i = 0; (Byte = A.ReadByte()) != -1; i++)
    {
    char by = (char)(Byte);
    Lines = Lines + by;
    }
    A.Close();
    FileStream B = new FileStream(@filee, FileMode.Create);
    foreach(char l in Lines)
    {
    Byte = (int)(l);
    B.WriteByte(Convert.ToByte(Byte));
    }
    B.Close();
    }
    }
    }
    }


    BC_Programmer


      Mastermind
    • Typing is no substitute for thinking.
    • Thanked: 1140
      • Yes
      • Yes
      • BC-Programming.com
    • Certifications: List
    • Computer: Specs
    • Experience: Beginner
    • OS: Windows 11
    Re: C# Copy file problem.
    « Reply #1 on: July 24, 2012, 01:53:46 PM »
    C#/.NET use Unicode.
    a Unicode char is 2 bytes in size. A byte is one... byte. Converting like this will not work. Additionally, when you have certain code points in a string it will create different unicode characters. You should not be using a string. I am not on Windows atm so I cannot test with an executable but you could use something like fc on the original and destination to see how the conversions are screwing up the executable.

    You are reading the entire file into memory, then writing that to a new file. Why? Why not do both at once? eg.

    Code: [Select]
    int bufferSize = 1024 * 64;
     using(FileStream is = new FileStream("source",FileMode.Open,FileAccess.Read))
    {
        using (FileStream fs = new FileStream("destination", FileMode.OpenOrCreate, FileAccess.Write))
        {

            int bytesRead = -1;
            byte[] bytes = new byte[bufferSize];

            while ((bytesRead = is.Read(bytes, 0, bufferSize)) > 0)
            {
                fs.Write(bytes, 0, bytesRead);
            }
            fs.Flush();
        }
    }
    Or, could do something really crazy like use the provided .NET framework method for this purpose:

    Code: [Select]
    File.Copy("Source","Destination");

    I was trying to dereference Null Pointers before it was cool.

    hibyy

      Topic Starter


      Rookie

      • Experience: Familiar
      • OS: Windows 8
      Re: C# Copy file problem.
      « Reply #2 on: July 26, 2012, 06:28:16 AM »
      Well so far I tried to test your code by compiling and it doesn't like your using statements here is the error.

      Copy.cs(10,22): error CS1031: Type expected
      Copy.cs(18,21): error CS1525: Invalid expression term 'is'

      I think I'll go through msdn.microsoft.com to see if I can find another way to do this. Your idea is really good thank you for the help =)

      EDIT: No the problem seems to not be the using but the first FileStream itself, I'll see if I can figure this out >.<

      EDIT AGAIN: okay the problem was the file streams name all it needed was to be changed from is to just about anything else >.>
      « Last Edit: July 26, 2012, 06:44:42 AM by hibyy »

      hibyy

        Topic Starter


        Rookie

        • Experience: Familiar
        • OS: Windows 8
        Re: C# Copy file problem.
        « Reply #3 on: July 26, 2012, 06:47:32 AM »
        Thank you, you solved my problem I re tested the code and it works =)

        BC_Programmer


          Mastermind
        • Typing is no substitute for thinking.
        • Thanked: 1140
          • Yes
          • Yes
          • BC-Programming.com
        • Certifications: List
        • Computer: Specs
        • Experience: Beginner
        • OS: Windows 11
        Re: C# Copy file problem.
        « Reply #4 on: July 26, 2012, 08:31:09 AM »
        EDIT AGAIN: okay the problem was the file streams name all it needed was to be changed from is to just about anything else >.>

        Yep, sorry about that. 'is' is a C# keyword, so using it as a variable name was a mistake on my part.
        I was trying to dereference Null Pointers before it was cool.