[英]How I can write a string in a txt file and used it for a message if a other user open the application?
I have a problem with write in a txt file. 我在txt文件中写入有问题。
My idea: 我的点子:
I want to load the form and create a file that is open. 我想加载表单并创建一个打开的文件。 If the Form is close the file close, too. 如果窗体关闭,文件也将关闭。 And in the Form_load method i create a second file with the info of the user and machinname, that I want write in the second file (user_log). 在Form_load方法中,我创建了第二个文件,其中包含用户名和机器名,我希望将其写入第二个文件(user_log)。 If a other user start the application I want that he see a messagebox with the name of the used user of this application. 如果其他用户启动该应用程序,我希望他看到一个消息框,其中包含该应用程序使用的用户的名称。
here my code: 这是我的代码:
private void Form1_Load(object sender, EventArgs e)
{
try
{
//Globale Datei wird erzeugt
var stream = File.Open("log.txt", FileMode.OpenOrCreate);
global_stream = stream;
//Benutzername und Rechnernummer wird ermitellt
string username = Benutzer.GetUsername();
string machine = tarasov_lib.Computer.GetMachineName();
// User_Log wird erstellt
using (var user_log = File.OpenWrite("user.txt"))
{
user_log.Write("Benutzername: " + username + " Rechnernummer: " + machine); //error
}
}
catch (Exception)
{
//Daten werden aus der User_log gelesen
TextDatei datei = new TextDatei();
string info = datei.ReadLine("user.txt", 0);
MessageBox.Show(info);
}
}
private void Form1_FormClosing(object sender, FormClosingEventArgs e)
{
global_stream.Close();
}
I think you can duplicate the log file into the temporary local path before load, and then read the content of the new one. 我认为您可以在加载之前将日志文件复制到临时本地路径中,然后读取新文件的内容。 Remember to close file after Read/Write immediately not on form-closing. 请记住,在读/写后立即关闭文件,而不是关闭窗体。
(Comment any idea to discuss about it) (评论讨论任何想法)
Hope to be helpful 希望对您有所帮助
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.