C# - StreamReader/StreamWriter - File used by another process -
first off, made sure dispose , close (both reader , writer) properly. method i'm doing i'm using using statement; , before this, manually used dispose , close method provided both writer , reader. both of these methods won't solve problem. secondly, point i'm suspecting, i'm processing lot of files both reading , writing. further, every time run program, program run deeper list of files supposed processed (ie, have 10 files, first time run program process first 2 before throwing error; second time run program process first 5 before throwing error , on). how supposed fix problem/bug? in advance!
edit - code attached
public static void fileprocessing(string initpath, string targetpath, string startline) { string line = null; string content = null; string nl = "\r\n"; using (streamreader reader = new streamreader(initpath)) { while (!reader.readline().contains(startline)) ; while (!reader.endofstream) { line = reader.readline(); // ignore empty line. if (line.length > 0) { if (!line.contains("<div")) content += line += nl; else break; } } } using (streamwriter writer = new streamwriter(targetpath)) { writer.write(content.trim()); } } /// <summary> /// create temporary text file correspond calendar department page /// , return path. /// </summary> public static string createfile(string path, string title) { string npath = path + title + ".txt"; if (!file.exists(npath)) file.createtext(npath); return npath; } /// <summary> /// return title of page. /// </summary> /// <param name="path"></param> public static string gettitle(string path) { string line; using (streamreader reader = new streamreader(path)) { (int = 0; < 31; ++i) { line = reader.readline(); // ignore empty line. if (line.length > 0) { if (line.contains("<h1>")) { return line.slice(4, -5); } } } return "null"; } } /// <summary> /// content temp path , /// output processed text destination path. /// </summary> /// <param name="temppath"></param> /// <param name="title"></param> public static void writeprocessedtext(string temppath, string targetpath) { string content = null; using (streamreader reader = new streamreader(temppath)) { string nl = "\r\n"; string line = null; while (!reader.endofstream) { line = htmlremoval.striptagsregex(reader.readline()) + nl; if (line.contains(" ")) line = line.replace(" ", " "); content += line; } } using (streamwriter writer = new streamwriter(targetpath)) { writer.write(content.trim()); } }
file.createtext(npath)
not creates filename on disk opens it. either close in createfile
method or change return stream.
public static string createfile(string path, string title) { string npath = path + title + ".txt"; if (!file.exists(npath)) file.createtext(npath); //<-- file not closed !!! return npath; }
Comments
Post a Comment