我正在尝试使用谷歌翻译翻译文本文件的数组,但它不翻译,如果我翻译它的文件

时间:2011-10-16 22:36:08

标签: c#

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using RavSoft.GoogleTranslator;
using System.Runtime.InteropServices;
using System.IO;

namespace Google_Translate
{
    public partial class Form1 : Form
    {
        DirectoryInfo dir1;
        FileInfo[] fi;
        string[] splittedFiles;
        string splitDirectory;
        OpenFileDialog openFileDialog1;
        FolderBrowserDialog fb;
        Translator t ;
        public Form1()
        {
            InitializeComponent();
            splitDirectory = @"d:\testsplit";
            openFileDialog1 = new OpenFileDialog();
            button1.Enabled = false;
            comboBox1.SelectedIndexChanged += new EventHandler(comboBox1_SelectedIndexChanged);
            comboBox2.SelectedIndexChanged += new EventHandler(comboBox2_SelectedIndexChanged);
            textBox1.TextChanged += new EventHandler(textBox1_TextChanged);
            t = new Translator();
        }

        private void Form1_Load(object sender, EventArgs e)
        {

        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == comboBox2.Text)
            {
                button1.Enabled = false;
                MessageBox.Show("Cannot translate to the same language , select a different target or source language");
            }
            else
            {
                if (textBox1.Text == "")
                {
                    button1.Enabled = false;
                }
                else
                {
                    t.SourceLanguage = comboBox1.Text;
                    t.TargetLanguage = comboBox2.Text;
                    button1.Enabled = true;
                }
            }

            if (comboBox1.Text == "" || comboBox2.Text == "" || comboBox1.Text == comboBox2.Text)
            {
                button1.Enabled = false;
            }
            else
            {
                if (textBox1.Text == "")
                {
                    button1.Enabled = false;
                }
                else
                {
                    t.SourceLanguage = comboBox1.Text;
                    t.TargetLanguage = comboBox2.Text;
                    button1.Enabled = true;
                }
            }
        }

        private void comboBox2_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (comboBox1.Text == comboBox2.Text)
            {
                button1.Enabled = false;
                MessageBox.Show("Cannot translate to the same language , select a different target or source language");
            }
            else
            {
                if (textBox1.Text == "")
                {
                    button1.Enabled = false;
                }
                else
                {
                    t.SourceLanguage = comboBox1.Text;
                    t.TargetLanguage = comboBox2.Text;
                    button1.Enabled = true;
                }
            }

            if (comboBox1.Text == "" || comboBox2.Text == "" || comboBox1.Text == comboBox2.Text)
            {
                button1.Enabled = false;
            }
            else
            {
                if (textBox1.Text == "")
                {
                    button1.Enabled = false;
                }
                else
                {
                    t.SourceLanguage = comboBox1.Text;
                    t.TargetLanguage = comboBox2.Text;
                    button1.Enabled = true;
                }
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            textBox1.Text = t.SourceText;
            t.Translate();
            textBox2.Text = t.Translation.ToString();
            textBox2.SelectionStart = 0;
            textBox2.SelectionLength = 0;
        }

        private void button2_Click(object sender, EventArgs e)
        {
            openFileDialog1.Title = "Select a text to translate";
            openFileDialog1.InitialDirectory = "c:\\";
            openFileDialog1.FileName = null;
            openFileDialog1.Filter = "Text File|*.txt";
            openFileDialog1.FilterIndex = 1;
            openFileDialog1.RestoreDirectory = true;
            DialogResult result1 = openFileDialog1.ShowDialog();
            string file1 = openFileDialog1.FileName;
            if (result1 == DialogResult.OK)
            {
                SplitFiles(file1, 1024 * 32, splitDirectory);
                dir1 = new DirectoryInfo(splitDirectory);
                fi = dir1.GetFiles("*.txt");
                for (int i = 0; i < fi.Length; i++)
                {
                    t.SourceText = fi[i].ToString();
                    t.SourceLanguage = comboBox1.Text;
                    t.TargetLanguage = comboBox2.Text;
                    t.Translate();
                }
                CombineFiles(@"d:\testsplit\newFileTranslated.txt", splitDirectory); // to change string newlargeFile to a new file name or the same file the user will select if the same name change file name and which directory to
                // save the file to using the current format sample in the CombineFile function \\
                // to check why its not translating the small files inside the for loop before combining them \\ 
                button1.Enabled = true;
            }
            if (result1 == DialogResult.Cancel)
            {
                if (file1 == "")
                {
                }
            }
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            if (textBox1.Text == "")
            {
                button1.Enabled = false;
            }
            else
            {
                if (comboBox1.Text == "" || comboBox2.Text == "" || comboBox1.Text == comboBox2.Text)
                {
                    button1.Enabled = false;
                }
                else
                {
                    button1.Enabled = true;
                }
            }
        }

        private bool SplitFiles(string largeFile, int preferredSize, string savePath)
        {
            bool bRet = false;
            if (File.Exists(largeFile))
            {
                StreamReader sr = null;
                StreamWriter sw = null;
                try
                {
                    Directory.CreateDirectory(savePath);

                    sr = new StreamReader(largeFile);
                    char[] allchars = sr.ReadToEnd().ToCharArray();
                    sr.Close();

                    int i = 0;
                    int position = 0;

                    while (position < allchars.Length)
                    {
                        int l = Math.Min(preferredSize, allchars.Length - position);
                        sw = new StreamWriter(Path.Combine(savePath, "File" + i.ToString("D4") + ".txt"));
                        sw.AutoFlush = true;

                        char[] buffer = new Char[l];
                        Array.Copy(allchars, position, buffer, 0, l);
                        sw.Write(buffer);

                        position += l;

                        i++;

                        sw.Close();

                        bRet = true;
                    }
                }
                catch
                {
                    bRet = false;
                }
                finally
                {
                    sr.Close();
                    sr = null;

                    sw.Close();
                    sw = null;
                }
            }

            return bRet;
        }



        private bool CombineFiles(string newLargeFile, string savePath)
        {
            bool bRet = false;

            if (Directory.Exists(savePath))
            {
                StreamReader sr = null;
                StreamWriter sw = null;

                try
                {
                    List<FileInfo> fList = new List<FileInfo>();
                    //fList.AddRange(new DirectoryInfo(savePath).GetFiles("*.txt").ToArray());
                    //fList = fList.OrderBy(a => a.Name).ToList();
                    fList.AddRange(new DirectoryInfo(savePath).GetFiles("*.txt").Where(a => a.FullName.ToLower().Equals(newLargeFile.ToLower()) == false).ToArray());
                    fList = fList.OrderBy(a => a.Name).ToList();

                    sw = new StreamWriter(newLargeFile);
                    sw.AutoFlush = true;

                    foreach (FileInfo fi in fList)
                    {
                        sr = new StreamReader(fi.FullName);
                        sw.Write(sr.ReadToEnd());
                        sr.Close();
                    }
                }
                catch
                {
                    bRet = false;
                }
                finally
                {
                    sr.Close();
                    sr = null;

                    sw.Close();
                    sw = null;
                }
            }

            return bRet;
        }
    }   
}

我现在使用StreamReader更改了button2 click事件,因此它将ReadToEnd();分割目录中的每个文本文件。

但它不起作用,它不会读取文件结束。

我在线上得到错误:r = new StreamReader(fi [i] .ToString());

错误说:FileNotFoundException:找不到文件'D:\ C-Sharp \ Google_Translate \ Google_Translate \ Google_Translate \ bin \ Debug \ File0000.txt'。

文本文件位于d:\ testsplit

无法弄清楚为什么它试图从C-Sharp ...目录中读取文件而不是d:\ testsplit

以下是更改过的片段的代码:

        if (result1 == DialogResult.OK)
        {
            SplitFiles(file1, 1024 * 32, splitDirectory);
            dir1 = new DirectoryInfo(splitDirectory);
            fi = dir1.GetFiles("*.txt");
            StreamReader r;
            for (int i = 0; i < fi.Length; i++)
            {
                r = new StreamReader(fi[i].ToString());
                string y = r.ReadToEnd();
                t.SourceLanguage = comboBox1.Text;
                t.TargetLanguage = comboBox2.Text;
                t.SourceText = y;
                t.Translate();
            }
            CombineFiles(@"d:\testsplit\newFileTranslated.txt", splitDirectory); // to change string newlargeFile to a new file name or the same file the user will select if the same name change file name and which directory to
            // save the file to using the current format sample in the CombineFile function \\
            // to check why its not translating the small files inside the for loop before combining them \\ 
            button1.Enabled = true;
        }

1 个答案:

答案 0 :(得分:0)

我想你需要

  • working directory 更改为包含文件的目录

    Environment.CurrentDirectory = @"d:\testsplit";
    
  • 或预先添加文件名的路径(将它们设为绝对@"d:\testsplit\test.txt"