0

I'm using those methods to upload files,

Client Side:

    private void Add(object sender, MouseButtonEventArgs e)
    {
        OpenFileDialog OFD = new OpenFileDialog();
        OFD.Multiselect = true;
        OFD.Filter = "Python (*.py)|*.py";
        bool? Result = OFD.ShowDialog();
        if (Result != null && Result == true)
            foreach (var File in OFD.Files)
                mylistbox.Items.Add(File);
    }
    private void Submit(object sender, MouseButtonEventArgs e)
    {
        foreach (var File in mylistbox.Items)
            Process(((FileInfo)File).Name.Replace(((FileInfo)File).Extension, string.Empty), ((FileInfo)File).OpenRead());
    }
    private void Process(string File, Stream Data)
    {
        UriBuilder Endpoint = new UriBuilder("http://localhost:5180/Endpoint.ashx");
        Endpoint.Query = string.Format("File={0}", File);

        WebClient Client = new WebClient();
        Client.OpenWriteCompleted += (sender, e) =>
        {
            Send(Data, e.Result);
            e.Result.Close();
            Data.Close();
        };
        Client.OpenWriteAsync(Endpoint.Uri);
    }
    private void Send(Stream Input, Stream Output)
    {
        byte[] Buffer = new byte[4096];
        int Flag;

        while ((Flag = Input.Read(Buffer, 0, Buffer.Length)) != 0)
        {
            Output.Write(Buffer, 0, Flag);
        }
    }

Server Side:

    public void ProcessRequest(HttpContext Context)
    {
        using (FileStream Stream = File.Create(Context.Server.MapPath("~/" + Context.Request.QueryString["File"].ToString() + ".py")))
        {
            Save(Context.Request.InputStream, Stream);
        }
    }

    private void Save(Stream Input, FileStream Output)
    {
        byte[] Buffer = new byte[4096];
        int Flag;
        while ((Flag = Input.Read(Buffer, 0, Buffer.Length)) != 0)
        {
            Output.Write(Buffer, 0, Flag);
        }
    }

My problem is that uploaded files have different Creation, Modification & Access Dates.
Why ?

Ahmed Ghoneim
  • 6,834
  • 9
  • 49
  • 79
  • Can you give an example of the dates? Are the different from the original file that got uploaded? – Andrew Grothe Mar 20 '13 at 16:59
  • Original dates are replaced by today's dates. – Ahmed Ghoneim Mar 21 '13 at 12:38
  • That's because you are creating a new file. I thought you meant that the Creation date on the new file was different from the Modification date on the new file. When you upload a file, you are essentially creating a new file, with duplicated contents. – Andrew Grothe Mar 21 '13 at 13:59

1 Answers1

0

When you upload a file, you are essentially creating a new file, with duplicated contents.

From your code:

using (FileStream Stream = File.Create(Context.Server.MapPath("~/" + Context.Request.QueryString["File"].ToString() + ".py")))
{
    Save(Context.Request.InputStream, Stream);
}

The File.Create is responsible for the new dates.

See this answer for information on preserving the dates.

Community
  • 1
  • 1
Andrew Grothe
  • 2,562
  • 1
  • 32
  • 48