0
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;
}
}
}
我現在改了THW BUTTON2單擊事件中使用的StreamReader所以它會ReadToEnd的();每個文本文件在分割目錄中。我嘗試使用谷歌翻譯翻譯文本文件的陣列,但它並沒有轉化,如果我翻譯一個文件,它
但它不起作用它不readl來結束文件。我在線上得到錯誤:r = new StreamReader(fi [i] .ToString());
錯誤說:FileNotFoundException:找不到文件'D:\ C-Sharp \ Google_Translate \ Google_Translate \ Google_Translate \ bin \ Debug \ File0000.txt'。
而且文本文件是在d:\ testsplit
無法弄清楚爲什麼它試圖讀取從C-夏普的文件...目錄,而不是從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;
}
請降低樣品。另外,不需要兩次發佈所有代碼 – sehe