Click here to Skip to main content
15,888,033 members
Home / Discussions / C#
   

C#

 
AnswerRe: list add method Pin
DaveyM6927-Jul-09 11:19
professionalDaveyM6927-Jul-09 11:19 
GeneralRe: list add method Pin
musefan27-Jul-09 11:20
musefan27-Jul-09 11:20 
GeneralRe: list add method Pin
DaveyM6927-Jul-09 13:17
professionalDaveyM6927-Jul-09 13:17 
GeneralRe: list add method Pin
Charlesh327-Jul-09 11:46
Charlesh327-Jul-09 11:46 
GeneralRe: list add method Pin
DaveyM6927-Jul-09 13:18
professionalDaveyM6927-Jul-09 13:18 
Questionencoded using the 16-bit UCS-2 character Pin
Flex.Me27-Jul-09 7:43
Flex.Me27-Jul-09 7:43 
AnswerRe: encoded using the 16-bit UCS-2 character Pin
Alan N27-Jul-09 9:34
Alan N27-Jul-09 9:34 
QuestionFunction to Find and Replace in C# Pin
GrgBalden27-Jul-09 6:39
GrgBalden27-Jul-09 6:39 
Hi All,

I've been putting together a short program too automatically to find and replace a string within all files of a directory.


For my first set of replacements I want to replace:

• “>” with “>”
• “&lt;” with “<”


Program.CS

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XMLReformat
{
    class Program
    {
        static void Main(string[] args)
        {

            //Console.WriteLine("Please Enter the directory of XML Documents:\n");
            //string userDir = Console.ReadLine();

            string userDir = "C:\\xmlreformat";

            try
            {
                string[] filePaths = Directory.GetFiles(userDir);
                foreach (string i in filePaths)
                {
                    // Call reformat operation

                    Reformat.Replace(i);

                }
                Console.WriteLine("Operation Succeeded \n \n Press Any Key to Exit");
                Console.ReadKey();
            }
            catch (Exception e)
            {
                Console.WriteLine("Directory Not Found:" + e.Message);
                Console.WriteLine("\n \n Press Any Key to Exit");
                Console.ReadKey();
            }

            

        }
    }
}



Reformat.CS

using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XMLReformat
{
    class Reformat
    {

        public static void Replace(string FName)
        {
            StreamReader File = new StreamReader(FName); // create new streamreader
            
            string fileContent = File.ReadToEnd(); // create new string, read file into string
            File.Close();


            StreamWriter NewFile = new StreamWriter(FName); // create new stream writer

            string fileContentCopy = fileContent;

            string istring1 = "&gt;";
            string ostring1 = ">";

            string istring2 = "&lt;";
            string ostring2 = "<";

            fileContentCopy.Replace(istring1, ostring1);
            fileContentCopy.Replace(istring2, ostring2);


            fileContent = fileContentCopy;

            NewFile.Write(fileContent);


        }


    }
}


However when I run the program it doesnt seem to work at all. Any ideas?

Thanks, George
AnswerRe: Function to Find and Replace in C# Pin
musefan27-Jul-09 6:50
musefan27-Jul-09 6:50 
GeneralRe: Function to Find and Replace in C# Pin
GrgBalden27-Jul-09 9:08
GrgBalden27-Jul-09 9:08 
AnswerRe: Function to Find and Replace in C# Pin
musefan27-Jul-09 9:50
musefan27-Jul-09 9:50 
AnswerRe: Function to Find and Replace in C# Pin
PIEBALDconsult27-Jul-09 9:36
mvePIEBALDconsult27-Jul-09 9:36 
GeneralRe: Function to Find and Replace in C# Pin
GrgBalden27-Jul-09 9:45
GrgBalden27-Jul-09 9:45 
GeneralRe: Function to Find and Replace in C# Pin
PIEBALDconsult27-Jul-09 10:37
mvePIEBALDconsult27-Jul-09 10:37 
QuestionBlinking form when minimaze [modified] Pin
gal00027-Jul-09 6:34
gal00027-Jul-09 6:34 
AnswerRe: Blinking form when minimaze Pin
musefan27-Jul-09 6:44
musefan27-Jul-09 6:44 
AnswerRe: Blinking form when minimaze Pin
Abhijit Jana27-Jul-09 6:48
professionalAbhijit Jana27-Jul-09 6:48 
GeneralRe: Blinking form when minimaze Pin
gal00027-Jul-09 8:42
gal00027-Jul-09 8:42 
AnswerRe: Blinking form when minimaze Pin
PIEBALDconsult27-Jul-09 9:39
mvePIEBALDconsult27-Jul-09 9:39 
GeneralRe: Blinking form when minimaze Pin
tamir90127-Jul-09 19:59
tamir90127-Jul-09 19:59 
QuestionCodeGenerator to generate async interface from sync? Pin
pioner27-Jul-09 5:56
pioner27-Jul-09 5:56 
QuestionProblems in deployment of c# BHO Pin
svt gdwl27-Jul-09 4:46
svt gdwl27-Jul-09 4:46 
QuestionHow to disable Task manager, esc,windows keys.................. Pin
Member 423560127-Jul-09 3:49
Member 423560127-Jul-09 3:49 
AnswerRe: How to disable Task manager, esc,windows keys.................. Pin
Manas Bhardwaj27-Jul-09 4:01
professionalManas Bhardwaj27-Jul-09 4:01 
AnswerRe: How to disable Task manager, esc,windows keys.................. Pin
Abhijit Jana27-Jul-09 4:07
professionalAbhijit Jana27-Jul-09 4:07 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.