使用 Winforms 的电子表格项目,无法复制文件错误

Spreadsheet Project with Winforms, Unable to Copy File error

我正在为软件工程 class 开发一个简单的 Excel 项目。这是我第一次编写 DLL 并使用 INotifyPropertyChanged。我收到这些错误:

错误 13 无法将 "SpreadsheetEngine\bin\Debug\SpreadsheetEngine.dll" 复制到 "bin\Debug\SpreadsheetEngine.dll"。超过重试次数 10。失败。"

错误 14 无法将文件 "SpreadsheetEngine\bin\Debug\SpreadsheetEngine.dll" 复制到 "bin\Debug\SpreadsheetEngine.dll"。该进程无法访问文件 'bin\Debug\SpreadsheetEngine.dll',因为它正被另一个进程使用。"

这些错误确实让我退缩了,如果能帮助我解决这些问题,我将不胜感激。

这是我的 DLL class:

 namespace SpreadsheetEngine
    {
    public abstract class Cell : INotifyPropertyChanged
    {
        private int RowIndex;
        private int ColumnIndex;
        protected string Text;
        protected string Value;

        public event PropertyChangedEventHandler PropertyChanged;

    //implement property changed notification
    protected void OnPropertyChanged(string name)
    {
        PropertyChangedEventHandler handler = PropertyChanged;
        if (handler != null)
        {
            handler(this, new PropertyChangedEventArgs(name));
        }
    }

    //Text property is a getter and setter for member variable
    public string CellText
    {
        get
        { return this.Text; }

        set
        {
            if (Text != value)
            {
                this.Text = value;
                OnPropertyChanged("Text");
            }
        }
    }

    //Value property is a string that represents the "evaluated" value.
    public string CellValue
    {
        get { return Value; }
    }

    abstract internal void SetValue(string value);


    //cell constructor sets column and row indices
    public Cell(int row_index, int column_index)
    {
        RowIndex = row_index;
        ColumnIndex = column_index;
    }

    //getters for row and column indices
    public int Row
    {
        get { return RowIndex; }
    }

    public int Column
    {
        get { return Column; }
    }
}

//this class inherits from cell so that we can instantiate and set value from inside 
public class RealCell : Cell
{
    //constructor calls the base class constructor
    public RealCell(int row_index, int column_index):base(row_index, column_index)
    {

    }


    //this is how we'll set the value property from the spreadsheet
    override internal void SetValue(string new_value)
    {
        Value = new_value;
    }
}

public class Spreadsheet 
{
    public int num_rows;
    public int num_columns;
    public RealCell[,] CellArray;

    public int ColumnCount()
    {
        return num_columns;
    }

    public int RowCount()
    {
        return num_rows;
    }


    //constructor with 2d array to hold cell objects
    public Spreadsheet(int rows, int columns)
    {
        num_rows = rows;
        num_columns = columns;

        RealCell[,] cell_array = new RealCell[num_rows, num_columns];
        //give all of the cells in our spreadsheet proper row and column indices
        for(int row = 1; row <= num_rows; row++)
        {
            //potential off by one error here at num_columns?
            for(char column = 'A'; column <= 'A' + num_columns; columns++)
            {
                RealCell cell = new RealCell(row, column);
                cell_array[row - 1, column - 65] = cell;
            }
        }
        CellArray = cell_array;
    }

    public event PropertyChangedEventHandler CellPropertyChanged;

    //implement property changed notification
    //TODO: implement CellPropertyChanged event
    protected void OnPropertyChanged(Cell cell, string name)
    {
        PropertyChangedEventHandler handler = CellPropertyChanged;
        if (handler == null)
        {
            handler(cell, new PropertyChangedEventArgs(name));
        }
    }

    void handler(object sender, PropertyChangedEventArgs e)
    {
        RealCell cell = sender as RealCell;

        string text = cell.CellText;
        if (text[0] != '=')
        {
            cell.SetValue(text);
        }
        else
        {
            cell.SetValue(text.Substring(1, text.Length));
            OnPropertyChanged(sender as Cell, "Cell Value");
        }
    }

    //function returns RealCell object 
    public RealCell get_cell(int row_index, int column_index)
    {
        if (row_index > num_rows || column_index > num_columns)
        {
            //there is no such cell. return null.
            return null;
        }
        else
        {
            return CellArray[row_index - 1, column_index - 65];
        }
    }
}

}

有一个打开的文件句柄阻止覆盖 SpreadsheetEngine.dll。当您构建解决方案时,无法按照错误消息中的说明重写此文件。最简单的解决方法是将 SpreadsheetEngine.dll 重命名为其他名称(例如 __SpreadsheetEngine.dll)。
您还可以使用 ProcessExplorer 查找和关闭文件句柄。

所以我找出了导致问题的原因。 当我尝试 运行 我的代码时,我注意到我的笔记本电脑风扇快疯了,我的电池寿命也很糟糕。打开我的任务管理器后,我看到即使在我关闭 Visual Studio 之后我的程序仍在 运行ning。 原来我在这里有一个无限循环:

for(char column = 'A'; column <= 'A' + num_columns; columns++)
        {
            RealCell cell = new RealCell(row, column);
            cell_array[row - 1, column - 65] = cell;
        }

我增加的是列而不是列。一个打字错误让我很伤心。