文件由其他进程使用

本文关键字:进程 其他 文件 | 更新日期: 2023-09-27 18:32:03

我正在使用 #C。如果我这样做

TextWriter tw = new StreamWriter("trades.txt");
// write a line of text to the file
tw.WriteLine(DateTime.Now);
// close the stream
tw.Close();

它将收到一个错误,指出该文件被其他进程使用:

[Henry 2014-11-26 21:10:45] ERROR: System.IO.IOException: Kan geen toegang krijg
en tot het bestand C:'Users'Jonathan'Desktop'HatBot'Bin'Debug'trades.txt omdat h
et wordt gebruikt door een ander proces.
   bij System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   bij System.IO.FileStream.Init(String path, FileMode mode, FileAccess access,
Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions
options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boole
an useLongPath, Boolean checkHost)
   bij System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   bij SteamBot.SimpleUserHandler.OnMessage(String message, EChatEntryType type)
 in c:'Users'Jonathan'Desktop'HatBot'SteamBot'SimpleUserHandler.cs:regel 160
   bij SteamBot.Bot.<HandleSteamMessage>b__9(FriendMsgCallback callback) in c:'U
sers'Jonathan'Desktop'HatBot'SteamBot'Bot.cs:regel 498
   bij SteamKit2.CallbackMsg.Handle[T](Action`1 handler)
   bij SteamBot.Bot.HandleSteamMessage(CallbackMsg msg) in c:'Users'Jonathan'Des
ktop'HatBot'SteamBot'Bot.cs:regel 488
  bij SteamBot.Bot.BackgroundWorkerOnDoWork(Object sender, DoWorkEventArgs doWo
rkEventArgs) in c:'Users'Jonathan'Desktop'HatBot'SteamBot'Bot.cs:regel 826
[Henry 2014-11-26 21:10:45] ERROR: System.IO.IOException: Kan geen toegang krijg
en tot het bestand C:'Users'Jonathan'Desktop'HatBot'Bin'Debug'trades.txt omdat h
et wordt gebruikt door een ander proces.
   bij System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   bij System.IO.FileStream.Init(String path, FileMode mode, FileAccess access,
Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions
options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy, Boole
an useLongPath, Boolean checkHost)
   bij System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access)
   bij SteamBot.SimpleUserHandler.OnMessage(String message, EChatEntryType type)
 in c:'Users'Jonathan'Desktop'HatBot'SteamBot'SimpleUserHandler.cs:regel 160
   bij SteamBot.Bot.<HandleSteamMessage>b__9(FriendMsgCallback callback) in c:'U
sers'Jonathan'Desktop'HatBot'SteamBot'Bot.cs:regel 498
   bij SteamKit2.CallbackMsg.Handle[T](Action`1 handler)
   bij SteamBot.Bot.HandleSteamMessage(CallbackMsg msg) in c:'Users'Jonathan'Des
ktop'HatBot'SteamBot'Bot.cs:regel 488
   bij SteamBot.Bot.BackgroundWorkerOnDoWork(Object sender, DoWorkEventArgs doWo
rkEventArgs) in c:'Users'Jonathan'Desktop'HatBot'SteamBot'Bot.cs:regel 826

怎么办?我想用命令来计算我已经得到的行数,但这会出现错误。

文件由其他进程使用

假设这是一个新文件,您可以尝试以下操作

string filePath = @"c:'"trades.txt"";
using (StreamWriter sw = new StreamWriter(filePath, true))
{
    tw.WriteLine(DateTime.Now.ToString());
    tw.Flush();
}

另一种方法可以做到这一点

using System.IO;
using(FileStream fileStream = new FileStream(@"c:'trades.txt", FileMode.Open))
{
   fileStream.Write(DateTime.Now.ToString());
   fileStream.Flush();
}