该进程无法访问文件'E:\ test.txt',因为它正由另一个进程使用

时间:2016-05-17 01:53:27

标签: c# winforms

我正在尝试从文本文件中删除无序import javafx.application.Application; import javafx.stage.Stage; import javafx.scene.Scene; import javafx.scene.control.Label; import javafx.scene.layout.BorderPane; import javafx.scene.layout.GridPane; import javafx.scene.layout.Pane; import javafx.scene.paint.Color; import javafx.scene.shape.Line; import javafx.scene.shape.Ellipse; public class TicTacToe extends Application { private char whoseTurn = 'X'; private Cell[][] cell = new Cell[3][3]; private Label lblStatus = new Label("X's turn to play"); @Override public void start(Stage primaryStage) { GridPane pane = new GridPane(); for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) pane.add(cell[i][j] = new Cell(), j, i); BorderPane borderPane = new BorderPane(); borderPane.setCenter(pane); borderPane.setBottom(lblStatus); Scene scene = new Scene(borderPane, 550, 470); primaryStage.setTitle("Wild TicTacToe"); primaryStage.setScene(scene); primaryStage.show(); primaryStage.setResizable(false); } /** Determine if the cell are all occupied */ public boolean isFull() { for (int i = 0; i < 3; i++) for (int j = 0; j < 3; j++) if (cell[i][j].getToken() == ' ') return false; return true; } /** Determine if the player with the specified token wins */ public boolean isWon(char token) { for (int i = 0; i < 3; i++) if (cell[i][0].getToken() == token && cell[i][1].getToken() == token && cell[i][2].getToken() == token) { return true; } for (int j = 0; j < 3; j++) if (cell[0][j].getToken() == token && cell[1][j].getToken() == token && cell[2][j].getToken() == token) { return true; } if (cell[0][0].getToken() == token && cell[1][1].getToken() == token && cell[2][2].getToken() == token) { return true; } if (cell[0][2].getToken() == token && cell[1][1].getToken() == token && cell[2][0].getToken() == token) { return true; } return false; } public class Cell extends Pane { private char token = ' '; public Cell() { setStyle("-fx-border-color: black"); this.setPrefSize(2000, 2000); this.setOnMouseClicked(e -> handleMouseClick()); } /** Return token */ public char getToken() { return token; } /** Set a new token */ public void setToken(char c) { token = c; if (token == 'X') { Line line1 = new Line(10, 10, this.getWidth() - 10, this.getHeight() - 10); line1.endXProperty().bind(this.widthProperty().subtract(10)); line1.endYProperty().bind(this.heightProperty().subtract(10)); Line line2 = new Line(10, this.getHeight() - 10, this.getWidth() - 10, 10); line2.startYProperty().bind( this.heightProperty().subtract(10)); line2.endXProperty().bind(this.widthProperty().subtract(10)); // Add the lines to the pane this.getChildren().addAll(line1, line2); } else if (token == 'O') { Ellipse ellipse = new Ellipse(this.getWidth() / 2, this.getHeight() / 2, this.getWidth() / 2 - 10, this.getHeight() / 2 - 10); ellipse.centerXProperty().bind( this.widthProperty().divide(2)); ellipse.centerYProperty().bind( this.heightProperty().divide(2)); ellipse.radiusXProperty().bind( this.widthProperty().divide(2).subtract(10)); ellipse.radiusYProperty().bind( this.heightProperty().divide(2).subtract(10)); ellipse.setStroke(Color.BLACK); ellipse.setFill(Color.WHITE); getChildren().add(ellipse); // Add the ellipse to the pane } } /* Handle a mouse click event */ private void handleMouseClick() { // If cell is empty and game is not over if (token == ' ' && whoseTurn != ' ') { setToken(whoseTurn); // Set token in the cell // Check game status if (isWon(whoseTurn)) { lblStatus.setText(whoseTurn + " won! The game is over."); whoseTurn = ' '; // Game is over } else if (isFull()) { lblStatus.setText("Draw! The game is over."); whoseTurn = ' '; // Game is over } else { // Change the turn whoseTurn = (whoseTurn == 'X') ? 'O' : 'X'; // Display whose turn lblStatus.setText(whoseTurn + "'s turn."); } } } } /** * The main method is only needed for the IDE with limited * JavaFX support. Not needed for running from the command line. */ public static void main(String[] args) { launch(args); } }到winForm中的white spaces序列,即

发件人

one-space

sagchjvcsj    kbschjsdchs      sudbjsdbl
sdvbchjbvsdjc    kbsadcsadk    kskbjdsdcksajdbc

我的实施是:

sagchjvcsj kbschjsdchs sudbjsdbl
sdvbchjbvsdjc kbsadcsadk kskbjdsdcksajdbc

我上线private void buttonBrowse_Click(object sender, EventArgs e) { Stream myStream; OpenFileDialog openFileDialogImage = new OpenFileDialog(); openFileDialogImage.Filter = "Text files | .txt"; openFileDialogImage.Multiselect = false; if (openFileDialogImage.ShowDialog() == System.Windows.Forms.DialogResult.OK) { if ((myStream = openFileDialogImage.OpenFile()) != null) { textBoxFileName.Text = openFileDialogImage.FileName; } } } private void buttonGo_Click(object sender, EventArgs e) { string path = textBoxFileName.Text; string s = string.Empty; using (StreamReader reader = new StreamReader(path, true)) { s = reader.ReadToEnd(); } string[] parts = s.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries); SaveFileDialog saveFileDialog = new SaveFileDialog(); saveFileDialog.ShowDialog(); string pathSave = saveFileDialog.FileName; File.CreateText(pathSave); using (StreamWriter sw = new StreamWriter(pathSave)) { sw.Write(parts); } } } } 的错误是:

using (StreamWriter sw = new StreamWriter(pathSave))

我下载了ProcessWorker以查看当前锁定The process cannot access the file 'E:\test.txt' because it is being used by another process. 的进程,但我没有看到任何使用它的进程。关于如何解决它的任何想法?

1 个答案:

答案 0 :(得分:3)

除了其他建议之外,您的问题是File.CreateText()会锁定,因此您需要释放锁定。我已将for File.CreateText()的调用包装在using语句中以释放锁定。

StreamWriter的输出存在问题,因此我根据您的问题进行了一些更改以获得预期的输出。

private void buttonGo_Click(object sender, EventArgs e)
{
    string path = textBoxFileName.Text;
    string s = string.Empty;
    string[] parts;
    using (StreamReader reader = new StreamReader(path, true))
    {
        parts = reader.ReadToEnd().Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
    }

    SaveFileDialog saveFileDialog = new SaveFileDialog();
    saveFileDialog.ShowDialog();
    string pathSave = saveFileDialog.FileName;

    using (File.CreateText(pathSave))
    { }

    using (StreamWriter sw = new StreamWriter(pathSave))
    {
        string result = string.Empty;
        foreach (string s in parts)
        {
            result += s + " ";
        }
        sw.Write(result);
    }
}