0

書き込み権限でファイルを開きます。しかし、同じ名前のファイルを再度開くと、例外が発生します。ここにコード:

// Check to see if file was uploaded
            if (FileMdl.PostedFile != null)
            {
                // Get a reference to PostedFile object
                HttpPostedFile myFile = FileMdl.PostedFile;

                // Get size of uploaded file
                int nFileLen = myFile.ContentLength;

                // make sure the size of the file is > 0
                if (nFileLen > 0)
                {
                    // Allocate a buffer for reading of the file
                    byte[] myData = new byte[nFileLen];

                    // Read uploaded file from the Stream
                    myFile.InputStream.Read(myData, 0, nFileLen);

                    // Create a name for the file to store
                    Constantes.strFilenameMdl = Path.GetFileName(myFile.FileName);

                    // Write data into a file
                    WriteToFile(Server.MapPath("Files//" + Constantes.strFilenameMdl), ref myData);
                    myFile.InputStream.Flush();
                    myFile.InputStream.Close();


                }
            }


    private void WriteToFile(string strPath, ref byte[] Buffer)
    {
        // Create a file
        FileStream newFile = new FileStream(strPath, FileMode.Create,FileAccess.ReadWrite,FileShare.ReadWrite);

        // Write data to the file
        newFile.Write(Buffer, 0, Buffer.Length);

        // Close file
        newFile.Flush();
        newFile.Close();
        newFile.Dispose();
    }

おそらく Flush または Close または Dispose で動作するはずですが、そうではありません。何か案が?

4

2 に答える 2