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

时间:2015-02-16 23:40:59

标签: c# excel winforms dll

我正在为一个软件工程类开发一个简单的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类:

 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];
        }
    }
}

}

2 个答案:

答案 0 :(得分:0)

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

答案 1 :(得分:0)

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

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

我正在增加列而不是列。这是一个错字给了我所有这些悲伤。