1

I want to make a program that searches a file for desired chars in words (letters č ć ž š), replaces them with c z s etc. and saves the file. In my attempt, however, I get some stupid signs, so that means it opens the file wrongly. When I try to add encoding.unicode it gives me errors (shown below). And one more question, how do I make a program which opens files by dragging them in an .exe file.

Error 3 The best overloaded method match for 'System.IO.File.Open(string, System.IO.FileMode, System.IO.FileAccess)' has some invalid arguments C:\Users\Vulisha\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 14 59 ConsoleApplication1

Error 4 Argument '3': cannot convert from 'System.Text.Encoding' to 'System.IO.FileAccess' C:\Users\Vulisha\AppData\Local\Temporary Projects\ConsoleApplication1\Program.cs 14 122 ConsoleApplication1

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


namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            using (StreamReader stream = new StreamReader(File.Open(@"C:\Users\Vulisha\Desktop\titl.txt", FileMode.Open)))
            {
                string fileText = stream.ReadToEnd();

                // Do your replacements
                fileText = fileText.Replace(@"č", @"c");
                fileText = fileText.Replace(@"ć", @"c");
                fileText = fileText.Replace(@"š", @"s");
                fileText = fileText.Replace(@"ž", @"z");
                fileText = fileText.Replace(@"đ", @"d");
                fileText = fileText.Replace(@"Č", @"C");
                fileText = fileText.Replace(@"Č", @"C");
                fileText = fileText.Replace(@"Š", @"S");
                fileText = fileText.Replace(@"Ž", @"Z");
                fileText = fileText.Replace(@"Đ", @"D");

                using (StreamWriter writer = new StreamWriter(File.Open(@"titl.txt", FileMode.Create)))
                {
                    // You do a create because the new file will have less characters than the old one
                    writer.Write(fileText);
                }
            }
        }
    }
}
4

1 回答 1

6

您需要更加小心括号的位置。你需要

new StreamWriter(File.Open(@"titl.txt", FileMode.Create), Encoding.Unicode)

但你写了

new StreamWriter(File.Open(@"titl.txt", FileMode.Create, Encoding.Unicode))

看到不同?

于 2013-05-28T22:25:56.150 回答