Using hostfile in C# I can block websites but I couldn't unblock them.
String path = #"C:\Windows\System32\drivers\etc\hosts";
StreamWriter sw = new StreamWriter(path, true);
sitetoblock = "\r\n127.0.0.1\t" + txtException.Text;
sw.Write(sitetoblock);
sw.Close();
MessageBox.Show(txtException.Text + " is blocked", "BLOCKED");
lbWebsites.Items.Add(txtException.Text);
txtException.Clear();
Here I need some help to unblock a specific site which is selected from listbox(lbWebsites). Is there a way to remove them from host file? I tried a lot and looked other solutions but something goes wrong in every solution.
You need to remove the lines you wrote to block the site. The most effective way is to read in the hosts file and write it again.
BTW, your method of blocking sites isn't going to be very effective. It might be okay for your usage scenario, but slightly technical people will know to look in the hosts file.
You may use a StreamReader to read the hosts file into a string. Then, initialize a new instance of a StreamWriter to write the content gathered back excluding the website you want to unblock.
Example
string websiteToUnblock = "example.com"; //Initialize a new string of name websiteToUnblock as example.com
StreamReader myReader = new StreamReader(#"C:\Windows\System32\drivers\etc\hosts"); //Initialize a new instance of StreamReader of name myReader to read the hosts file
string myString = myReader.ReadToEnd().Replace(websiteToUnblock, ""); //Replace example.com from the content of the hosts file with an empty string
myReader.Close(); //Close the StreamReader
StreamWriter myWriter = new StreamWriter(#"C:\Windows\System32\drivers\etc\hosts"); //Initialize a new instance of StreamWriter to write to the hosts file; append is set to false as we will overwrite the file with myString
myWriter.Write(myString); //Write myString to the file
myWriter.Close(); //Close the StreamWriter
Thanks,
I hope you find this helpful :)
You can do this:
String path = #"C:\Windows\System32\drivers\etc\hosts";
System.IO.TextReader reader = new StreamReader(path);
List<String> lines = new List<String>();
while((String line = reader.ReadLine()) != null)
lines.Add(line);
Then you have all lines of your hosts-file in the lines list. Afterwards, you can search for the site you want to unblock and remove it from the list until the desired site is not anymore in the list:
int index = 0;
while(index != -1)
{
index = -1;
for(int i = 0; i< lines.Count(); i++)
{
if(lines[i].Contains(sitetounblock))
{
index = i;
break;
}
}
if(index != -1)
lines.RemoveAt(i);
}
After you have done that, just convert the cleaned list to a normal string:
String content = "";
foreach(String line in lines)
{
content += line + Environment.NewLine;
}
Then just write content to the file ;)
Written in my head, so no guarantee on having no errors :P
Related
Here I want to delete line in a textfiles containg specific string like "21309#003" where item1 is a filename but It shows runtime exception that item1 (file) is already use in some process.How I Solve this problem.I am new in .net C#.
private void button1_Click(object sender, EventArgs e)
{
var selectedItems = listBox1.SelectedItems.Cast<String>().ToList();
foreach (var item in selectedItems)
{
listBox1.Items.Remove(item);
}
foreach (var item1 in selectedItems)
{
listBox1.Items.Remove(item1);
string line = null;
//string line_to_delete = "the line i want to delete";
using (StreamReader reader = new StreamReader(item1))
//item1= "C:\\IMP2711\\textpresent.txt"
{
using (StreamWriter writer = new StreamWriter(item1))
{
while ((line = reader.ReadLine()) != null)
{
//if (String.Compare(line, #"*21349#003*") == 0)
//if (!line.Contains("21349#003") )
if (!line.StartsWith("21349#003"))
{**strong text**
writer.WriteLine(line);
}
}
}
You are reading and writing to the same file at the same time.
var item2 = item1;
If the file is not to big you can read the lines into memory and then write the lines you want to keep back to the file. We can even simplify your code a little bit.
File.WriteAllLines(item1,
File.ReadLines(item1).Where(l => !l.StartsWith("21349#003")).ToList());
Another option if the file is very large is to write to a temporary file. Delete the original and then rename the temporary.
var tmp = Path.GetTempFileName();
File.WriteAllLines(tmp, File.ReadLines(item1).Where(l => !l.StartsWith("21349#003")));
File.Delete(item1);
File.Move(tmp, item1);
If your file is small first read it to memory and then try to write on it, you have two stream on the same file, a file can share between multiple streams but you can not modify a file when it is open by another stream, if your file is huge and you can not moved to memory you can create a temp file and write to temp file when your reading finished replacing original file with temp file and removing temp file.
There's some process that's locking the file c:\imp2711\textpresent.txt. You have to find and kill it.
To find it out, please refer to this question: https://superuser.com/questions/117902/find-out-which-process-is-locking-a-file-or-folder-in-windows
string curetn = Environment.CurrentDirectory;
string path = curetn.ToString() + #"\DATA\SaveGame.txt";
Console.WriteLine(path);
TextReader tr = new StreamReader(path);
Hello, I am making a text-adventure, and I do not like having all my save files, and mp3 file in the same place as my application. I would like for the files to be in a folder. I want to be able to use StreamWriter and StreamReader, to be able to write and read files that are in a folder. This file is also in a distributable folder, not just in the Visual Studios Projects folders. I have tried everything I can, and this is what I have. I also have one of these for StreamWriter. Please help!
Edit:
The thing that does not work, is that it does not read the lines, and assigns them to a variable. I have it in a try-catch, and it catches, and displays the error message that I wrote.
If you are looking for simply read and write lines from file you can try this
using (StreamReader sr = new StreamReader(path))
{
while (!sr.EndOfStream)
{
sr.ReadLine();
}
}
string s;
using (StreamWriter sw = new StreamWriter(path))
{
sw.WriteLine(s);
}
So basically what you want to do is read the text file:
string data[] = File.ReadAllLines(path); // Read the text file.
var x = data[1]; // Replace the '1' with the line number you want.
Console.WriteLine(x);
This is a good way to read the text file, I think it's better than opening a stream.
You can also write to it, so every time you want to save, just do this:
// When you want to write:
File.WriteAllText(path, "");
File.AppendAllText(path, "Add a data line" + Environment.NewLine); // Environment.NewLine adds a line.
Keep appending text to the file for the data you need.
So im trying to close a file (transactions.txt) that has been open that i've used to read into a textbox and now I want to save back to the file but the problem debug says that the file is in use so I need to find a way to close it. Can anyone help me with this? Thanks!
SearchID = textBox1.Text;
string ID = SearchID.ToString();
bool idFound = false;
int count = 0;
foreach (var line in File.ReadLines("transactions.txt"))
{
//listView1.Items.Add(line);
if (line.Contains(ID))
{
idFound = true;
}
//Displays Transactions if the variable SearchID is found.
if (idFound && count < 8)
{
textBox2.Text += line + "\r\n";
count++;
}
}
}
private void SaveEditedTransaction()
{
SearchID = textBox1.Text;
string ID = SearchID.ToString();
bool idFound = false;
int count = 0;
foreach (var lines in File.ReadLines("transactions.txt"))
{
//listView1.Items.Add(line);
if (lines.Contains(ID))
{
idFound = true;
}
if (idFound)
{
string edited = File.ReadAllText("transactions.txt");
edited = edited.Replace(lines, textBox2.Text);
File.WriteAllText("Transactions.txt", edited);
}
The problem here is that File.ReadLines keeps the file open while you read it, since you've put the call to write new text to it inside the loop, the file is still open.
Instead I would simply break out of the loop when you find the id, and then put the if-statement that writes to the file outside the loop.
This, however, means that you will also need to maintain which line to replace in.
So actually, instead I would switch to using File.ReadAllLines. This reads the entire file into memory, and closes it, before the loop starts.
Now, pragmatic minds might argue that if you have a lot of text in that text file, File.ReadLines (that you're currently using) will use a lot less memory than File.ReadAllLines (that I am suggesting you should use), but if that's the case then you should switch to a database, which would be much more suited to your purpose anyway. It is, however, a bit of an overkill for a toy project with 5 lines in that file.
Use StreamReader directly with the using statement, for example:
var lines = new List<string>();
using (StreamReader reader = new StreamReader(#"C:\test.txt")) {
var line = reader.ReadLine();
while (line != null) {
lines.Add(line);
line = reader.ReadLine();
}
}
By using the using statement the StreamReader instance will automatically be disposed of after it's done with it.
You can try with this:
File.WriteAllLines(
"transactions.txt",
File.ReadAllLines("transactions.txt")
.Select(x => x.Contains(ID) ? textBox2.Text : x));
It works fine, but if the file is big you have to find other solutions.
You can use the StreamReader class instead of the methods of the File class. In this way you can use, Stream.Close() and Stream.Dispose().
Right now I'm building a game application using c#, which will require loading from text file for the game script. (It's quite a simple visual novel game)
Now when the main form loads, i load the script from the file script.txt
And i declared :
StringReader reader = new StringReader(script);
as a global variable there
Now in the middle of the game where the reader is in the middle of the string script,
I need to append starting from next line of the reader.
Basically what I want to achieve:
Append all texts from "news.txt" to script starting from reader.ReadLine() [i.e. in the middle of the string script]
What will be the most efficient solution to achieve this?
What I know:
StreamReader sr = new StreamReader("news.txt");
string news = sr.ReadToEnd();
//Now how to append 'news' to reader.ReadLine() ??
Edit for more clarification (sorry, this is my first time asking here) :
I will try to explain more about what i'm trying to achieve here.
What i'm having right now :
//global variables
string script;
StringReader reader;
//during form_load
StreamReader sr = new StreamReader("script.txt");
script = sr.ReadToEnd();
reader - new StringReader(script);
//And as the game progresses, I keep on implementing reader.ReadLine()..
//At one point, the program will ask the user, do you want to watch the news?
DialogResult dialogResult = MessageBox("Do you want to watch the news?", , MessageBoxButtons.YesNo
if(dialogResult == DialogResult.Yes)
{
StreamReader newsSr = new StreamReader("news.txt");
string news = newsSr.ReadToEnd();
//now I want to append the contents of 'news' to the string 'script' after reader.ReadLine() - any best way to implement this?
}
One possible way (and i think it's the worst way as well) is by introducing one count variable, to get the starting position of the last reader.ReadLine(),
and execute the desired result using Insert like follows :
script = script.Insert(startIndex, news)
You cannot write into a StringReader.
However, if I understand your latest question I think you want this.
StreamReader sr = new StreamReader("news.txt");
string news = string.Empty;
string line = sr.ReadLine();
while (line != null)
{
news += line;
news += someOtherString;
line = sr.ReadLine();
}
Aside I wouldn't do this with string concatenation. I would do this with a StringBuilder.
Just load the file into memory using File.ReadAllLines().
You can then access this as a string array, without worrying about readers, writers, streams etc.
For example:
// load files as arrays
string[] scriptLinesArray = File.ReadAllLines("script.txt");
string[] newsLinesArray = File.ReadAllLines("news.txt");
// convert arrays to lists
var script = new List<string>(scriptLinesArray);
var news = new List<string>(newsLinesArray );
// append news list to script list
script.AddRange(news);
in the end I was able to resolve this problem.
This is what I used (in case anyone wanna know :))
//I'm using a switch statement, in case reader.ReadLine() == "#(morningnews)"
dialogResult = MessageBox.Show("Do you want to watch the news?", , MessageBoxButtons.YesNo);
if(dialogResult = DialogResult.Yes)
{
StreamReader sr = new StreamReader(directoryName + "\\morningactivities\\morningnews1.txt");
string news = sr.ReadToEnd();
script = script.Replace("#(morningnews)", "#(morningnews)\n" + news);
reader = new StringReader(script);
while (reader.ReadLine() != "#(morningnews)")
continue;
loadNextScript();
}
Thanks for everyone who helped, it gave me the inspiration to actually came up with this.
I have a big file with some text, and I want to split it into smaller files.
In this example, What I do:
I open a text file let's say with 10 000 lines into it
I set a number of package=300 here, which means, that's the small file limit, once a small file has 300 lines into it, close it, open a new file for writing for example (package2).
Same, as step 2.
You already know
Here is the code from my function that should do that. The ideea (what I dont' know) is how to close, and open a new file once it has reached the 300 limit (in our case here).
Let me show you what I'm talking about:
int nr = 1;
package=textBox1.Text;//how many lines/file (small file)
string packnr = nr.ToString();
string filer=package+"Pack-"+packnr+"+_"+date2+".txt";//name of small file/s
int packtester = 0;
int package= 300;
StreamReader freader = new StreamReader("bigfile.txt");
StreamWriter pak = new StreamWriter(filer);
while ((line = freader.ReadLine()) != null)
{
if (packtester < package)
{
pak.WriteLine(line);//writing line to small file
packtester++;//increasing the lines of small file
}
else if (packtester == package)//in this example, checking if the lines
//written, got to 300
{
packtester = 0;
pak.Close();//closing the file
nr++;//nr++ -> just for file name to be Pack-2;
packnr = nr.ToString();
StreamWriter pak = new StreamWriter(package + "Pack-" + packnr + "+_" + date2 + ".txt");
}
}
I get this errors:
Cannot use local variable 'pak' before it is declared
A local variable named 'pak' cannot be declared in this scope because it would give a different meaning to 'pak', which is already used in a 'parent or current' scope to denote something else
Try this:
public void SplitFile()
{
int nr = 1;
int package = 300;
DateTime date2 = DateTime.Now;
int packtester = 0;
using (var freader = new StreamReader("bigfile.txt"))
{
StreamWriter pak = null;
try
{
pak = new StreamWriter(GetPackFilename(package, nr, date2), false);
string line;
while ((line = freader.ReadLine()) != null)
{
if (packtester < package)
{
pak.WriteLine(line); //writing line to small file
packtester++; //increasing the lines of small file
}
else
{
pak.Flush();
pak.Close(); //closing the file
packtester = 0;
nr++; //nr++ -> just for file name to be Pack-2;
pak = new StreamWriter(GetPackFilename(package, nr, date2), false);
}
}
}
finally
{
if(pak != null)
{
pak.Dispose();
}
}
}
}
private string GetPackFilename(int package, int nr, DateTime date2)
{
return string.Format("{0}Pack-{1}+_{2}.txt", package, nr, date2);
}
Logrotate can do this automatically for you. Years have been put into it and it's what people trust to handle their sometimes very large webserver logs.
Note that the code, as written, will not compile because you define the variable pak more than once. It should otherwise function, though it has some room for improvement.
When working with files, my suggestion and the general norm is to wrap your code in a using block, which is basically syntactic sugar built on top of a finally clause:
using (var stream = File.Open("C:\hi.txt"))
{
//write your code here. When this block is exited, stream will be disposed.
}
Is equivalent to:
try
{
var stream = File.Open(#"C:\hi.txt");
}
finally
{
stream.Dispose();
}
In addition, when working with files, always prefer opening file streams using very specific permissions and modes as opposed to using the more sparse constructors that assume some default options. For example:
var stream = new StreamWriter(File.Open(#"c:\hi.txt", FileMode.CreateNew, FileAccess.ReadWrite, FileShare.Read));
This will guarantee, for example, that files should not be overwritten -- instead, we assume that the file we want to open doesn't exist yet.
Oh, and instead of using the check you perform, I suggest using the EndOfStream property of the StreamReader object.
This code looks like it closes the stream and re-opens a new stream when you hit 300 lines. What exactly doesn't work in this code?
One thing you'll want to add is a final close (probably with a check so it doesn't try to close an already closed stream) in case you don't have an even multiple of 300 lines.
EDIT:
Due to your edit I see your problem. You don't need to redeclare pak in the last line of code, simply reinitialize it to another streamwriter.
(I don't remember if that is disposable but if it is you probably should do that before making a new one).
StreamWriter pak = new StreamWriter(package + "Pack-" + packnr + "+_" + date2 + ".txt");
becomes
pak = new StreamWriter(package + "Pack-" + packnr + "+_" + date2 + ".txt");