Click here to Skip to main content
15,912,504 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
first of all im really new to this and new with c# sorry about my english..well im about to make an application for school project, im creating a simple cashregister app thing..

so i wanted to make a button to save every text/number value in texboxes i've created
the program runs but, im getting this error after click the save button i've created
An unhandled exception of type 'System.ArgumentException' occurred in mscorlib.dll
Absolute path information is required. occurred

at this line
FileIOPermission filePermission =
                    new FileIOPermission(FileIOPermissionAccess.AllAccess, "data.xml");


What I have tried:

form1.cs
save button
private void btnsave_Click_1(object sender, EventArgs e)
       {
           if (File.Exists("data.xml"))
           {
               File.SetAttributes("data.xml", FileAttributes.Normal);
               FileIOPermission filePermission =
                   new FileIOPermission(FileIOPermissionAccess.AllAccess, "data.xml");

               using (FileStream stream = new FileStream("data.xml", FileMode.Create))
               {
                   try
                   {
                       Information info = new Information();
                       info.Data1 = txtBasoSU.Text;
                       info.Data2 = txtBasoC.Text;
                       info.Data3 = txtBasoM.Text;
                       info.Data4 = txtBasoB.Text;
                       info.Data5 = txtMieA.Text;
                       info.Data6 = txtMieAB.Text;
                       info.Data7 = txtServisMinuman.Text;
                       info.Data8 = txtServisMakanan.Text;
                       info.Data9 = txtServisBungkus.Text;
                       info.Data10 = txtTotalSpajak.Text;
                       info.Data11 = txtTotalPajak.Text;
                       info.Data12 = txtTotal.Text;

                       SaveXML.SaveData(info, "data.xml");

                   }
                   catch (Exception ex)
                   {
                       MessageBox.Show(ex.Message);
                   }
               }
           }
       }

load button
private void btnload_Click(object sender, EventArgs e)
        {
            if (File.Exists("data.xml"))
            {
                using (var stream = new FileStream("data.xml", FileMode.Open))
                {
                    var xs = new XmlSerializer(typeof(Information));
                    FileStream read = new FileStream("data.xml", FileMode.Open, FileAccess.ReadWrite);
                    Information info = (Information)xs.Deserialize(read);
                    // FileStream read = new FileStream("data.xml", FileMode.Open, FileAccess.ReadWrite);
                    //Information info = (Information)xs.Deserialize(read);

                    txtBasoSU.Text = info.Data1;
                    txtBasoC.Text = info.Data2;
                    txtBasoM.Text = info.Data3;
                    txtBasoB.Text = info.Data4;
                    txtMieA.Text = info.Data5;
                    txtMieAB.Text = info.Data6;

                    txtServisMinuman.Text = info.Data7;
                    txtServisMakanan.Text = info.Data8;
                    txtServisBungkus.Text = info.Data9;
                    txtTotalSpajak.Text = info.Data10;
                    txtTotalPajak.Text = info.Data11;
                    txtTotal.Text = info.Data12;
                }
            }
        }


information.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;

namespace CS_Sistem_Managemen_Restoran
{
    public class Information
    {
        private string data1;
        private string data2;
        private string data3;
        private string data4;
        private string data5;
        private string data6;

        private string data7;
        private string data8;
        private string data9;
        private string data10;
        private string data11;
        private string data12;
        
        
        public string Data1
        {
            get { return data1; }
            set { data1 = value; }
        }

        public string Data2
        {
            get { return data2; }
            set { data2 = value; }
        }
        public string Data3
        {
            get { return data3; }
            set { data3 = value; }
        }

        public string Data4
        {
            get { return data4; }
            set { data4 = value; }
        }

        public string Data5
        {
            get { return data5; }
            set { data5 = value; }
        }

        public string Data6
        {
            get { return data6; }
            set { data6 = value; }
        }

        public string Data7
        {
            get { return data7; }
            set { data7 = value; }
        }

        public string Data8
        {
            get { return data8; }
            set { data8 = value; }
        }

        public string Data9
        {
            get { return data9; }
            set { data9 = value; }
        }

        public string Data10
        {
            get { return data10; }
            set { data10 = value; }
        }

        public string Data11
        {
            get { return data11; }
            set { data11 = value; }
        }

        public string Data12
        {
            get { return data12; }
            set { data12 = value; }
        }

        
    }
}


saveXML.cs
using System.Text;
using System.Threading.Tasks;
using System.Xml.Serialization;
using System.IO;

namespace CS_Sistem_Managemen_Restoran
{
    public class SaveXML
    {
        public static void SaveData(object obj, string filename)
        {
            XmlSerializer sr = new XmlSerializer(obj.GetType());
            TextWriter writer = new StreamWriter(filename);
            sr.Serialize(writer, obj);
            writer.Close();
        }
    }
}
Posted
Updated 11-Aug-18 22:45pm

1 solution

The error is pretty self explanatory, if you look at it.
Absolute path information is required

Is coming on this line:
FileIOPermission filePermission =
                    new FileIOPermission(FileIOPermissionAccess.AllAccess, "data.xml");
Because "data.xml" is a relative path - and it's relative to wherever the current app is running at this precise moment. The DLL doesn't know where that is, or at the very least isn't sure that it knows, so it specifically tests for an absolute location when you specify the file name.

It's a bit like being in a library, and talking to someone on the phone. When you tell them you are reading "this book" that doesn't help them come and look at it because "this book" is a relative address: you need to say: "Floor 3, IT Section, Row 14, Shelf 3, 'Fundamentals of OOPs programming'" to provide them with an absolute address for them to walk up the stairs and find it.

So replace "data.xml" with it's absolute address: @"C:\I keep stuff here\XML files\data.xml" and it should get rid of the error.

By the way, have a look at this: Where should I store my data?[^] - it may be a little advanced for you, but it suggests some sensible places to keep data, and shows you how to get the path to them.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900