我正在使用#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
怎么办?我想用一个命令来计算线条,我已经知道了。但这是错误的......
答案 0 :(得分:0)
假设这是一个新文件,您可以尝试以下
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();
}