2014-11-26 4 views
-3

Im using #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

вы пробовали, выдавшего 'Flush () 'Метод до кодирования .. также есть весь код, который вы используете ..? показать весь соответствующий код, а также посмотреть на повторный факторинг кода, чтобы обернуть ваш 'TextWriter' вокруг' using' – MethodMan

+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(); 
} 
Смежные вопросы