1

我试图使用 using 但在使用它时遇到错误。

public void Save(string path , bool Locked , PictureBox pb)
        {
            string fn;
            string t = Path.GetFileNameWithoutExtension(wo_name);
            if (File.Exists(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name))
            {
                using  (string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name))
                {
                    File.Delete(f);
                }


                fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name;
            }
            else
            {
                fn = path + "\\" + "DATABASE" + "\\" + wo_name + "\\" + wo_name + ".txt";
            }
            OptionsFile setting_file = new OptionsFile(fn);
            setting_file.SetKey("File Name", fn);
            setting_file.SetKey("Version", version);
            setting_file.SetKey("Button Lock", Locked.ToString());

            setting_file.SetKey("picturebox.Width", pb.Width.ToString());
            setting_file.SetKey("picturebox.Height", pb.Height.ToString());
                setting_file.SetListFloatKey("Coordinates_X", woc.Point_X);
                setting_file.SetListFloatKey("Coordinates_Y", woc.Point_Y);

                setting_file.SetListIntKey("ConnectionStart", connectionStart);
                setting_file.SetListIntKey("ConnectionEnd", connectionEnd);


        }

在上面的这个保存功能中,我做了:

using  (string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name))
                    {
                        File.Delete(f);
                    }

在它之前只有 string f = ....Path.... 和 Fiel.Delete 我刚刚添加了 using 但在 using 上出现错误:'string': type used in a using statement must be implicitly convertible to 'System.IDisposable'

4

1 回答 1

0

类上有CloseorDispose方法OptionsFile吗?在退出SaveandLoad函数之前,您应该确保调用其中一个或另一个。如果你不这样做,那么你可能不得不等到setting_file实例被垃圾回收之后,文件上的锁才会被释放。如果您不分配大量内存,则可能需要一段时间!

如果OptionsFile.Dispose存在,则将您的使用包含OptionsFile在一个using语句中,该语句将Dispose在语句末尾自动为您调用,即使抛出异常也是如此。

    public void Save(string path , bool Locked , PictureBox pb) 
    { 
        string fn; 
        string t = Path.GetFileNameWithoutExtension(wo_name); 
        if (File.Exists(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name)) 
        { 
            string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name); 
            File.Delete(f); 

            fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name; 
        } 
        else 
        { 
            fn = path + "\\" + "DATABASE" + "\\" + wo_name + "\\" + wo_name + ".txt"; 
        } 

        using (OptionsFile setting_file = new OptionsFile(fn))
        {
            setting_file.SetKey("File Name", fn); 
            setting_file.SetKey("Version", version); 
            setting_file.SetKey("Button Lock", Locked.ToString()); 

            setting_file.SetKey("picturebox.Width", pb.Width.ToString()); 
            setting_file.SetKey("picturebox.Height", pb.Height.ToString()); 
            setting_file.SetListFloatKey("Coordinates_X", woc.Point_X); 
            setting_file.SetListFloatKey("Coordinates_Y", woc.Point_Y); 

            setting_file.SetListIntKey("ConnectionStart", connectionStart); 
            setting_file.SetListIntKey("ConnectionEnd", connectionEnd); 
        }
    } 

    public void Load( string path) 
    { 
        string fn = path + "\\" + wo_name;  
        using (OptionsFile setting_file = new OptionsFile(fn))
        {
            woc.Point_X = new List<float>(); 
            woc.Point_Y = new List<float>(); 

            woc.Point_X = setting_file.GetListFloatKey("Coordinates_X"); 
            woc.Point_Y = setting_file.GetListFloatKey("Coordinates_Y"); 


            connectionStart = new List<int>(); 
            connectionEnd = new List<int>(); 

            connectionStart = setting_file.GetListIntKey("ConnectionStart"); 
            connectionEnd = setting_file.GetListIntKey("ConnectionEnd"); 

            lockObject = setting_file.GetKey("Button Lock"); 
        }
    } 

如果OptionsFile.Close存在,则将您的使用包含OptionsFile在一个块中,这样即使抛出异常try/finally,您也可以确保被调用。Close

    public void Save(string path , bool Locked , PictureBox pb) 
    { 
        string fn; 
        string t = Path.GetFileNameWithoutExtension(wo_name); 
        if (File.Exists(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name)) 
        { 
            string f = Path.Combine(path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name); 
            File.Delete(f); 

            fn = path + "\\" + "DATABASE" + "\\" + t + "\\" + wo_name; 
        } 
        else 
        { 
            fn = path + "\\" + "DATABASE" + "\\" + wo_name + "\\" + wo_name + ".txt"; 
        } 

        OptionsFile setting_file = null;
        try
        {
            setting_file = new OptionsFile(fn);

            setting_file.SetKey("File Name", fn); 
            setting_file.SetKey("Version", version); 
            setting_file.SetKey("Button Lock", Locked.ToString()); 

            setting_file.SetKey("picturebox.Width", pb.Width.ToString()); 
            setting_file.SetKey("picturebox.Height", pb.Height.ToString()); 
            setting_file.SetListFloatKey("Coordinates_X", woc.Point_X); 
            setting_file.SetListFloatKey("Coordinates_Y", woc.Point_Y); 

            setting_file.SetListIntKey("ConnectionStart", connectionStart); 
            setting_file.SetListIntKey("ConnectionEnd", connectionEnd); 
        }
        finally
        {
            if (setting_file != null)
                setting_file.Close();
        }
    } 

    public void Load( string path) 
    { 
        string fn = path + "\\" + wo_name;  

        OptionsFile setting_file = null;
        try
        {
            setting_file = new OptionsFile(fn);

            woc.Point_X = new List<float>(); 
            woc.Point_Y = new List<float>(); 

            woc.Point_X = setting_file.GetListFloatKey("Coordinates_X"); 
            woc.Point_Y = setting_file.GetListFloatKey("Coordinates_Y"); 


            connectionStart = new List<int>(); 
            connectionEnd = new List<int>(); 

            connectionStart = setting_file.GetListIntKey("ConnectionStart"); 
            connectionEnd = setting_file.GetListIntKey("ConnectionEnd"); 

            lockObject = setting_file.GetKey("Button Lock"); 
        }
        finally
        {
            if (setting_file != null)
                setting_file.Close();
        }
    } 

如果您没有一个Dispose或一个Close方法,那么您需要修改您的OptionsFile类以实现IDisposable或拥有一个Close方法,然后您将关闭或处置任何Stream基于实例的实例。

于 2012-07-15T04:42:59.373 回答