IoException while writing in text file in c# - c#

I have text file which i need to update according to the regex match but as soon as my program tries to write a line into text file it is giving following error..
The process cannot access the file 'D:\Archieve\20140123.text' because it is being used by another process.
Here is my C# code..
static void Main(string[] args)
{
string textfilename="";
string strDateTime = DateTime.Now.ToString("yyyyMMdd");
string strformatedatetime = DateTime.Now.ToString("yyyy/MM/dd");
if (strDateTime != "") {
string loc = "D:\\Archieve\\";
string date=strDateTime;
string text=".text";
textfilename = loc + date + text;
File.Create(textfilename);
}
string pattern = "^" + strformatedatetime + ".*";
string FileToCopy = "D:\\ipdata.txt";
string NewCopy =textfilename;
StringBuilder sb = new StringBuilder("");
List<string> newLines = new List<string>();
if (System.IO.File.Exists(FileToCopy) == true)
{
string[] lines = File.ReadAllLines(FileToCopy);
foreach (string line in lines)
{
if (Regex.IsMatch(line, pattern))
{
sb.Append(line + System.Environment.NewLine);
TextWriter tsw = new StreamWriter(textfilename,true);
//Writing text to the file.
tsw.WriteLine(sb);
//Close the file.
tsw.Close();
}
}
}
}
I am getting above defined error at this line of code...
TextWriter tsw = new StreamWriter(textfilename,true);
Where am i going wrong ?

You don't need to have a separate instruction to create a file.
The StreamWriter will take care of it: Here is the description of the constructor you user
> Initializes a new instance of the StreamWriter class for the specified
> file by using the default encoding and buffer size. If the file
> exists, it can be either overwritten or appended to. If the file does
> not exist, this constructor creates a new file.

Use File.Create(textfilename).Close();
As the error message suggests

Related

File manipulation - procedure problems

I've been given a few different sets of procedures for various different things in C# under file manipulation.
I have forgotten basically how to call or use the procedures and so they are pretty much useless to me until I figure out how. Apologies for sounding stupid but I've done as much searching as I can and I can't relate other sources to my problem.
Here's a procedure I've been given:
void readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
}
Now I understand completely what this and all the other procedures do, yet I forget how to use them in main.
Here's what I currently have in main:
string path = "C:\\Users\\Joe\\Documents\\General\\College\\Computer Science\\Coding\\TextFileWork\\textFile.txt";
string readFile;
readFile = readFromTextFile(path);
Now the problem I'm having is understanding how to use the procedure back into main to return the read file. The string readFile is what I am trying to append the read text into, however I don't know how I should call the function in order to append it. Some basic help should suffice, thanks!
EDIT:
Here's the entire code I currently have (C# Console Application)
namespace TextFileWork_03._03._18
{
class Program
{
static void Main(string[] args)
{
string path = "C:\\Users\\Joe\\Documents\\General\\College\\Computer
Science\\Coding\\TextFileWork\\textFile.txt";
string readFile;
readFromTextFile(readFile);
if (File.Exists(path) == true)
{
//Create a file to write to.
Console.WriteLine(path + " Exists");
}
else
{
Console.WriteLine(path + " File not found");
}
FileInfo fi = new FileInfo(path);
FileStream fs = fi.Create();
fs.Close();
if (File.Exists(path) == true)
{
//Create a file to write to.
Console.WriteLine(path + " Now exists");
}
else
{
Console.WriteLine(path + " File still not found");
}
}
static void readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
}
}
}
To make your code work you'll need a return value from your method, otherwise you cannot say:
mysomthing = readFromTextFile
So, lets return a string value:
There is one problem: do you want to return a single line or just the whole file?
Here's the whole file version:
static string readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
StringBuilder sb = new StringBuilder();
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
sb.AppendLine(line);
}
//close the file
sr.Close();
return sb.ToString();
}
update this answer is not valid, I'll just keep it for a few minutes for reference.
You should have given more of your code to actually make this a valid question. Nevertheless, I'll try to help you out.
Your procedure (we call it methods in C#, (strongly related to functions)), lives in a class, lets call it Foo, but you can look it up in your code. Just scroll up: its the first blue class you'll see.
public class Foo //this is your class
{
void readFromTextFile(string path)
{
StreamReader sr = new StreamReader(path);
//Read the first line of text
string line = sr.ReadLine();
//Continue to read until you reach end of file
while (line != null)
{
//write the line to console window
Console.WriteLine(line);
//Read the next line
line = sr.ReadLine();
}
//close the file
sr.Close();
}
}
To call it, from your Main method, you'll need an object
void Main()
{
string path = "C:\\Users\\Joe\\Documents\\General\\College\\ComputerScience\\Coding\\TextFileWork\\textFile.txt";
string readFile;
Foo fooObject = new Foo(); //create a new Foo
readFile = fooObject.readFromTextFile(path);
}

Only write data to text file if the line doesnt match any others

I want to add data into a text file based on a specific output. It will read an XML file and write a certain line to a text file. If the data is already written into the text file, I don't want to write it again.
Code
public void output(string folder)
{
string S = "Data" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xml";
//Trades.Save(S);
string path = Path.Combine(folder, S);
Console.WriteLine(path);
XDocument f = new XDocument(Trades);
f.Save(path);
string[] lines = File.ReadAllLines(path);
File.WriteAllLines(path, lines);
using (System.IO.StreamWriter file = new System.IO.StreamWriter(
#"H:\Test" + DateTime.Now.ToString("yyMMdd") + ".txt", true))
{
foreach (string line in lines)
{
if (line.Contains("CertainData"))
{
file.WriteLine(line);
if (File.ReadAllLines(path).Any(x => x.Equals(line)))
{
}
else
{
string[] tradeRefLines = File.ReadAllLines(path);
File.WriteAllLines(path, tradeRefLines); ;
}
}
}
}
}
The problem is it will still write the line even if the data is exactly the same elsewhere. I don't want duplicate lines.
Any advice?
Edit
The CertainData is a reference number.
I have a bunch of files that have data in them and the piece I want to seperate and put into a text file is CertainData field, which will have a reference number.
Sometimes the files I get sent will have the same formatted information inside it with the CertainData appearing in them for reference.
When i run this programme, if the text file i have already contains the "CertainData" reference number inside it, i dont want it to be written
If you need anymore clarification let me know and i will update the post
Try with this LINQ:
var previousLines = new HashSet<string>();
File.WriteAllLines(destPath, File.ReadLines(sourcePath)
.Where(line => previousLines.Add(line)));
EDITED :
public void output(string folder)
{
string S = "Data" + DateTime.Now.ToString("yyyyMMddHHmm") + ".xml";
//Trades.Save(S);
string path = Path.Combine(folder, S);
Console.WriteLine(path);
XDocument f = new XDocument(Trades);
f.Save(path);
string[] lines = File.ReadAllLines(path);
File.WriteAllLines(path, lines);
bool isExist = false;
using (System.IO.StreamWriter file = new System.IO.StreamWriter(#"H:\Test" + DateTime.Now.ToString("yyMMdd") + ".txt", true))
{
foreach (string line in lines)
{
if (line.Contains("CertainData"))
{
isExist = true;
}
}
if (!isExist)
{
File.AppendAllText(path, "CertainData" + Environment.NewLine);
}
}
}

Writing a string array to a File but it just prints System.String[]

I have written this code but has some problems:
const int maxPeopleInFile = 2;
using (StreamReader reader = new StreamReader(#"c:\mytest\SortedTest.txt"))
{
string[] columnheaders = reader.ReadLine().Split(',');
List<string> listKeeper = new List<string>();
int fileNumber = 1;
while (reader.Peek() > 0)
{
string[] currentRowValues = reader.ReadLine().Split(',');
string id = currentRowValues[2];
if (listKeeper.Count < maxPeopleInFile || (listKeeper.Count() <= maxPeopleInFile && listKeeper.Contains(id)))
{
if (!listKeeper.Contains(id))
{
listKeeper.Add(id);
}
var writer = File.CreateText("file_" + fileNumber + ".txt");
writer.Write(currentRowValues);
writer.Close();
}
else // new file
{
fileNumber++;
listKeeper = new List<string>();
var writer = File.CreateText("file_" + fileNumber + ".txt");
writer.Write(currentRowValues);
}
}
}
Problems:
1: The files generated don't have the line I have read in string[] currentRowValues = reader.ReadLine().Split(',');
What is being written to the file is one line and it is the text System.String[]
Since currentRowValues is an array, this call
writer.Write(currentRowValues);
is equivalent to
writer.Write(currentRowValues.ToString());
which produces the output that you see (i.e. System.String[]) because ToString() does not iterate the individual string values.
You can write the whole array into a file in one shot using File.WriteAllLines, like this:
File.WriteAllLines("file_" + fileNumber + ".txt", currentRowValues);
(From a comment) [I] want to put [the exact row that I have read from original file] in a new file exactly like it used to be in the original file
Then you should use string.Join to undo the effects of the string.Split:
writer.Write(string.Join(",", currentRowValues));
It's because currentRowValues won't be serialized back to a comma-separated list of values by just giving it to FileStream.Write, because it writes the return value of ToString which is the object type name.
One possible approach might be using string.Join:
writer.Write(string.Join(",", currentRowValues));
your problem is in this snippet:
writer.Write(currentRowValues);
try:
writer.Write(currentRowValues[0]);
or
writer.Write(currentRowValues[1]);
depending on your needs
You need to provide a string value to the Write() method, not a string[].
I would suggest the following:
StringBuilder builder = new StringBuilder();
foreach (var currentValue in currentRowValues)
builder.Append(currentValue);
writer.Write(builder.ToString());
Note: Don't forget to add the namespace to be able to use StringBuilder.

read IEnumerable text list and issue move commands C#

I am trying to read a list in text file and move the files source to target directory. But as I am trying to read this file using for statement it's giving me following error :
foreach statement cannot operate on variables of type
System.IO.StreamReader because System.IO.StreamReader does not
contain a public definition for GetEnumerator
Not sure if this is issue with file creation or some alternate approach is needed here to read and then move files.
Please advise.
Here's the code:
static void Main(string[] args)
{
create_source_fileList();
string source_dir = System.Configuration.ConfigurationSettings.AppSettings["SourceDir"];
string target_dir = System.Configuration.ConfigurationSettings.AppSettings["TargetDir"];
string dpath = target_dir + "Diff" + ".txt";
TextWriter df = new StreamWriter(dpath);
DirectoryInfo sourceinfo = new DirectoryInfo(source_dir);
DirectoryInfo targetinfo = new DirectoryInfo(target_dir);
string[] source_f_list = File.ReadAllLines(target_dir + "Source_File_List.txt");
string[] target_f_list = File.ReadAllLines(target_dir + "Target_File_List.txt");
IEnumerable<String> file_list_diff = source_f_list.Except(target_f_list);
string diff_list = string.Join(Environment.NewLine, file_list_diff);
df.WriteLine(string.Join(Environment.NewLine, file_list_diff));
df.Close();
System.IO.StreamReader file_read = new System.IO.StreamReader(target_dir + "\\Diff.txt");
if (!Directory.Exists(targetinfo.FullName))
{
Directory.CreateDirectory(targetinfo.FullName);
}
/*foreach (FileInfo fi in sourceinfo.GetFiles())
{
fi.CopyTo(Path.Combine(targetinfo.ToString(), fi.Name), true);
}*/
foreach (string file in file_read) // Error With For Loop
{
}
create_target_fileList();
}
Yes, you can't use foreach directly with StreamReader. It doesn't have the required members for foreach to work.
Options:
Use File.ReadLines instead:
string path = Path.Combine(targetDir, "Diff.txt");
foreach (string file in File.ReadLines(path))
{
// ...
}
Repeatedly call ReadLine on your StreamReader, which should be in a using statement and is simply obtained using File.OpenText:
string path = Path.Combine(targetDir, "Diff.txt");
using (TextReader reader = File.OpenText(path))
{
string file;
while ((file = reader.ReadLine()) != null)
{
// ...
}
}
Note that I've made the variable names a bit cleaner too - idiomatic variable names in C# are things like sourceFileList rather than source_f_list.
Additionally, I'd encourage you to use File.WriteAllText earlier on, rather than the way you're using df (without a using statement, and opening it much earlier than you need to).

C# windows forms application:How can I replace a single line in a textfile without deleting the original file?

I am making a project that uses streamreader and streamwriter, Is it possible that I only replace or save a text in an specific line only without affecting the other lines?
if I make like this
streamreader sr = new streamreader(#"txtfile");
list<string> lines = new list<string>();
while (!sr.EndOfStream)
sr.readline();
{
lines.Add(sr.ReadLine();
}
//put in textbox
sr.close();
{
streamwriter sw = new streamwriter(#"txtfile");
sw.WriteLine(textBox1.text);
sw.close();
}
this is just a sample, but Is it possible that I use list also un streamwriter?
If you want a one line solution (code golf :) ) you can use
string path = #"C:\Test.txt";
string lineToReplace = "Relpace This Line";
string newLineValue = "I Replaced This Line";
File.WriteAllLines(path, File.ReadAllLines(path).Select(line => line.Equals(lineToReplace) ? newLineValue : line));
You cannot just change a line as such but you can to ReadAllLines, find the line you want to change, change it and write all of it to the file again :
StringBuilder newFile = new StringBuilder();
string temp = "";
string[] file = File.ReadAllLines(#"txtfile");
foreach (string line in file)
{
if (line.Contains("string you want to replace"))
{
temp = line.Replace("string you want to replace", "New String");
newFile.Append(temp + "\r\n");
continue;
}
newFile.Append(line + "\r\n");
}
File.WriteAllText(#"txtfile", newFile.ToString());
Read the file into memory, changing the line(s) you want to change, close the reader, open the file for writing, write the new contents of the file out.

Categories

Resources