tags:

views:

538

answers:

2

So let's say I have a program with just a text box and an okay button. The user types in whatever word he wants, and when he clicks ok, it opens a specific file called Test.doc and CTRL+F for the word "test" and replaces it with whatever the user entered into the text box. How can I open said file and replace instances of the word test with the user's defined word?

A: 

A number of things:

I'd recommend using a FileDialog to get the file's location. This lets you select the file to edit, but also gives you functionality to only show the file types that you want to handle in this program.

If you're handling .doc's, I'd suggest you look into VSTO and opening word docs. Here's a guide I found after a quick search. I'd suggest using it as a place to start, but you'll need to look around for more specifics.

Lastly, the string.Replace("", ""); method is probably very helpful in the CTRL-F functionality. You should be able to extract a string of the text from whatever document you're analyzing and use that method.

Drew McGhie
+2  A: 

Ignoring the format of the document, you could literally use the folowing for any type of file:

        var contents = System.IO.File.ReadAllText(@"C:\myDoc.doc");
        contents = contents.Replace("Test", "Tested");
        System.IO.File.WriteAllText(@"C:\myDoc.doc", contents);

The best way would be to use the ms office interop library though.

Andrew

REA_ANDREW