【问题标题】:File.ReadAllText and after File.WriteAllTextFile.ReadAllText 和 File.WriteAllText 之后
【发布时间】:2014-04-08 17:14:23
【问题描述】:

我的问题是,当我运行此代码时,它告诉我“File.WriteAllText”行 procedure.pas 正在被另一个程序使用。

script = File.ReadAllText(Application.StartupPath + "\\Procedure.pas");
decryptedstring = Crypter.Decrypt(script, sSecretKey);

decryptedstring = decryptedstring.Replace("_" + Firma + "_", "_" + txtFirma.Text + "_");
decryptedstring = decryptedstring.Replace("_" + DonemNo + "_", "_" + txtDonemNo.Text + "_");
decryptedstring = decryptedstring.Replace("FIRMNR=" + Firma + " ", "FIRMNR=" + txtFirma.Text + " ");
encryptedstring = Crypter.Encrypt(decryptedstring, sSecretKey);
File.WriteAllText(Application.StartupPath + "\\Procedure.pas", encryptedstring);

【问题讨论】:

  • 你对 Application,StartupPath 有写权限吗?
  • 在 WriteAllText 之前放置一个断点并使用 Windows 资源监视器来检查谁在使用这个文件 - 只是为了好奇......
  • 我猜你也想写的文件被另一个程序使用了?! ://

标签: c# file


【解决方案1】:

您正在写入的文件可能是使用共享锁打开的,该锁允许其他进程读取数据但不允许写入。因此,当您打开文件进行写入时,尽管文件一直被另一个进程打开,但它会失败并出现异常。

有关详细信息,请参阅FileShare 枚举。

【讨论】:

    猜你喜欢
    • 2012-03-20
    • 1970-01-01
    • 2012-07-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-13
    相关资源
    最近更新 更多