When i run the second form it occurs a error "The Process cannot access the file
C:\user\Maxs\Desktop\xml\Student.soap because it is being used by another process"
Can anybody fix this


Form1 Creating soap file

using System.IO;
using Students;// Student DLL file 
using System.Runtime.Serialization.Formatters.Soap;

namespace TestAps
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Student s = new Student();
            s.age = int.Parse(txtAge.Text);
            s.name = txtUname.Text;

  



            SoapFormatter sf = new SoapFormatter();
            FileStream fs = new FileStream(@"C:\Users\Maxs\Desktop\XML\Student.soap",FileMode.OpenOrCreate,FileAccess.Write);
            sf.Serialize(fs,s);
}}}

Form 2 Read Soap file

using System.IO;
using Students;// Student DLL file 
using System.Runtime.Serialization.Formatters.Soap;

namespace TestAps
{
    public partial class Form2 : Form
    {
        public Form2()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            Student s = new Student();
FileStream(@"C:\Users\Maxs\Desktop\XML\Student.bin",FileMode.Open,FileAccess.Read);
            
            //s=(Student)bf.Deserialize(fs);
            //BinaryFormatter bf = new BinaryFormatter();
            SoapFormatter sf = new SoapFormatter();
            FileStream fs = new FileStream(@"C:\Users\Maxs\Desktop\XML\Student.soap",FileMode.Open,FileAccess.Read);
            s=(Student)sf.Deserialize(fs);
            


            label1.Text=s.name;
            label2.Text = Convert.ToString(s.age);
        }
    }
}

You neeed to close the filestream in form1.

[Edit] As it is now it will only close when the GC disposes of the filestream.

You neeed to close the filestream in form1.

[Edit] As it is now it will only close when the GC disposes of the filestream.

Can u tell me how to close it ,

fs.Close(); after sf.Serialize(fs,s);

fs.Close(); after sf.Serialize(fs,s);

Thanks a lot .it is working now thnx nick.crane

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.