问题描述
我是编程新手,我有一个问题。如果我有两个功能,一个创建一个文本文件并将其写入,另一个打开同一个文本文件并从中读取。
我得到的错误是:
我尝试为每个功能设置单独的计时器,但仍然无法正常工作。我认为最好的方法是函数2直到函数1结束才开始。
非常感谢!
Mike
源代码:
公共表格1( ){
InitializeComponent();
System.Timers.Timer timerButtona1 =新的System.Timers.Timer();
timerButtona1.Elapsed + = new ElapsedEventHandler(tickTimera1);
timerButtona1.Interval = 3003;
timerButtona1.Enabled = true;
}
私有异步void tickTimera1(object source,ElapsedEventArgs e){
function1();
function2();
}
void function1(){
List< string> linki = new List< string>();
linki.Add( https://link1.net/);
linki.Add( https://link2.net/);
linki.Add( https://link3.net/);
List< string> fileNames = new List< string>();
fileNames.Add( name1);
fileNames.Add( name2);
fileNames.Add( name3);
for(int x = 0; x< fileNames.Count; x ++){
GET(linki [x],fileNames [x]);
//System.Threading.Thread.Sleep(6000);
}
}
异步void GET(字符串链接,字符串fileName){
var ODGOVOR =等待PRENOS_PODATKOV.GetStringAsync(link);
File.WriteAllText(@ C:\Users\ ... \ + fileName + .txt,ODGOVOR);
}
void function2(){
string originalText = File.ReadAllText(@ C:\Users\ ... \fileName.txt,编码。默认);
动态modifiedText = JsonConvert.DeserializeObject(originalText);
//然后我从相同的文本文件中读取并使用其中的一些数据。
}
编辑后必须关闭文件。
var myFile = File.Create(myPath);
// myPath = C:\file.txt
myFile.Close();
//关闭文本文件,例如file.txt
//您可以立即编写阅读功能。.
关闭后您可以再次使用它(供阅读)
I am new to programming and I have a question. If I have two functions, one creates a text file and writes into it, while the other opens the same text file and reads from it.
The error I get is:
I have tried setting seperate timers to each of the functions but it still does not work. I think the best way would be that the function two does not start until function one ends.
Can you help me achieve this?Thank you very much!Mike
Source code:
public Form1() {
InitializeComponent();
System.Timers.Timer timerButtona1 = new System.Timers.Timer();
timerButtona1.Elapsed += new ElapsedEventHandler(tickTimera1);
timerButtona1.Interval = 3003;
timerButtona1.Enabled = true;
}
private async void tickTimera1(object source, ElapsedEventArgs e) {
function1();
function2();
}
void function1() {
List<string> linki = new List<string>();
linki.Add("https://link1.net/");
linki.Add("https://link2.net/");
linki.Add("https://link3.net/");
List<string> fileNames = new List<string>();
fileNames.Add("name1");
fileNames.Add("name2");
fileNames.Add("name3");
for (int x = 0; x < fileNames.Count; x++) {
GET(linki[x], fileNames[x]);
//System.Threading.Thread.Sleep(6000);
}
}
async void GET(string link, string fileName) {
var ODGOVOR = await PRENOS_PODATKOV.GetStringAsync(link);
File.WriteAllText(@"C:\Users\...\" + fileName + ".txt", ODGOVOR);
}
void function2() {
string originalText = File.ReadAllText(@"C:\Users\...\fileName.txt", Encoding.Default);
dynamic modifiedText = JsonConvert.DeserializeObject(originalText);
//then i then i read from the same text files and use some data from it..
}
You will have to close the file after editing it.
var myFile = File.Create(myPath);
//myPath = "C:\file.txt"
myFile.Close();
//closes the text file for eg. file.txt
//You can write your reading functions now..
After closing it you can again use it(for reading)
这篇关于System.IO.IOException:'该进程无法访问文件'@ .txt',因为它正在被另一个进程使用。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!