Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 文件被其他进程使用_C#_Count - Fatal编程技术网

C# 文件被其他进程使用

C# 文件被其他进程使用,c#,count,C#,Count,我用的是#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 g

我用的是#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
[Henry 2014-11-26 21:10:45]错误:System.IO.IOException:Kan geen-toegang-krijg
完整的BEST和C:\Users\Jonathan\Desktop\HatBot\Bin\Debug\trades.txt omdat h
这是一个过程。
bij System.IO.\uuu Error.WinIOError(Int32 errorCode,字符串maybeFullPath)
bij System.IO.FileStream.Init(字符串路径、文件模式、文件访问、,
Int32权限、布尔用户权限、文件共享共享、Int32缓冲区大小、文件选项
选项、安全属性、字符串msgPath、布尔bFromProxy、布尔
一个useLongPath,布尔checkHost)
bij System.IO.FileStream..ctor(字符串路径、文件模式、文件访问)
bij SteamBot.SimpleUserHandler.OnMessage(字符串消息,EChatEntryType类型)
在c:\Users\Jonathan\Desktop\HatBot\SteamBot\SimpleUserHandler.cs:regel 160中
c:\U中的bij-SteamBot.Bot.b\U 9(FriendMsgCallback回调)
sers\Jonathan\Desktop\HatBot\SteamBot\Bot.cs:regel 498
bij SteamKit2.CallbackMsg.Handle[T](操作'1处理程序)
c:\Users\Jonathan\Des中的bij-SteamBot.Bot.HandleSteamMessage(CallbackMsg-msg)
ktop\HatBot\SteamBot\Bot.cs:regel 488
bij-SteamBot.Bot.BackgroundWorkerOnDoWork(对象发送方,DoWorkEventArgs-doWo
c:\Users\Jonathan\Desktop\HatBot\SteamBot\Bot.cs:regel 826
[Henry 2014-11-26 21:10:45]错误:System.IO.IOException:Kan geen toegang krijg
完整的BEST和C:\Users\Jonathan\Desktop\HatBot\Bin\Debug\trades.txt omdat h
这是一个过程。
bij System.IO.\uuu Error.WinIOError(Int32 errorCode,字符串maybeFullPath)
bij System.IO.FileStream.Init(字符串路径、文件模式、文件访问、,
Int32权限、布尔用户权限、文件共享共享、Int32缓冲区大小、文件选项
选项、安全属性、字符串msgPath、布尔bFromProxy、布尔
一个useLongPath,布尔checkHost)
bij System.IO.FileStream..ctor(字符串路径、文件模式、文件访问)
bij SteamBot.SimpleUserHandler.OnMessage(字符串消息,EChatEntryType类型)
在c:\Users\Jonathan\Desktop\HatBot\SteamBot\SimpleUserHandler.cs:regel 160中
c:\U中的bij-SteamBot.Bot.b\U 9(FriendMsgCallback回调)
sers\Jonathan\Desktop\HatBot\SteamBot\Bot.cs:regel 498
bij SteamKit2.CallbackMsg.Handle[T](操作'1处理程序)
c:\Users\Jonathan\Des中的bij-SteamBot.Bot.HandleSteamMessage(CallbackMsg-msg)
ktop\HatBot\SteamBot\Bot.cs:regel 488
bij-SteamBot.Bot.BackgroundWorkerOnDoWork(对象发送方,DoWorkEventArgs-doWo
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();
}

在编码之前,您是否尝试过发出
Flush()
方法。。这也是您正在使用的所有代码。。?同时显示所有相关代码,并查看重新分解代码以使用将您的
TextWriter
包装在
周围。好吧,看起来另一个进程已经锁定了文件。