【问题标题】:I'm trying to translate array of text file using google translate but it doesn't translate, if i translate one file it does我正在尝试使用谷歌翻译翻译文本文件数组,但它不翻译,如果我翻译一个文件它会翻译
【发布时间】:2011-10-16 22:36:08
【问题描述】:
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 点击事件,因此它将 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;
        }

【问题讨论】:

  • 请减少样本。此外,无需将所有代码发布两次

标签: c#


【解决方案1】:

我想你需要

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

    Environment.CurrentDirectory = @"d:\testsplit";
    
  • 或在文件名前添加路径(使它们成为绝对路径@"d:\testsplit\test.txt"

【讨论】:

    猜你喜欢
    • 2019-05-20
    • 1970-01-01
    • 1970-01-01
    • 2022-01-17
    • 1970-01-01
    • 2013-01-29
    • 2011-12-26
    • 2015-05-19
    • 1970-01-01
    相关资源
    最近更新 更多