forked from stefankueng/EvImSync
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathENScriptWrapper.cs
153 lines (137 loc) · 5.86 KB
/
ENScriptWrapper.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// EvImSync - A tool to sync Evernote notes to IMAP mails and vice versa
// Copyright (C) 2010 - Stefan Kueng
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace EveImSync
{
/// <summary>
/// Wrapper for the ENScript.exe tool
/// </summary>
public class ENScriptWrapper
{
/// <summary>
/// path to the ENScript.exe
/// </summary>
private string exePath;
/// <summary>
/// the full path to ENScript.exe
/// </summary>
public string ENScriptPath
{
get
{
return exePath;
}
set
{
exePath = value;
}
}
/// <summary>
/// Lists all the notebooks in Evernote
/// </summary>
/// <returns>List of notebook names</returns>
public List<string> GetNotebooks()
{
List<string> notebooks = new List<string>();
ProcessStartInfo processStartInfo = new ProcessStartInfo(this.exePath, "listNotebooks");
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
if (processStarted)
{
StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();
while (outputReader.Peek() >= 0)
{
notebooks.Add(outputReader.ReadLine());
}
}
return notebooks;
}
/// <summary>
/// Exports the specified notebook to the specified file
/// </summary>
/// <param name="notebook">the notebook to export</param>
/// <param name="exportFile">the file to export the notebook to</param>
/// <returns>true if successful, false in case of an error</returns>
public bool ExportNotebook(string notebook, string exportFile)
{
bool ret = false;
if (!File.Exists(exePath))
return ret;
ProcessStartInfo processStartInfo = new ProcessStartInfo(this.exePath, "exportNotes /q \"notebook:" + notebook + "\" /f " + exportFile);
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
if (processStarted)
{
StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();
ret = (process.ExitCode == 0) && File.Exists(exportFile);
}
return ret;
}
/// <summary>
/// Imports all the notes in the export file to the
/// specified notebook in Evernote
/// </summary>
/// <param name="notesPath">the path to the export file</param>
/// <param name="notebook">the notebook where the export file should be imported to</param>
/// <returns>true if successful, false in case of an error</returns>
public bool ImportNotes(string notesPath, string notebook)
{
bool ret = false;
ProcessStartInfo processStartInfo = new ProcessStartInfo(this.exePath, "importNotes /n " + notebook + " /s " + notesPath);
processStartInfo.UseShellExecute = false;
processStartInfo.ErrorDialog = false;
processStartInfo.RedirectStandardError = true;
processStartInfo.RedirectStandardInput = true;
processStartInfo.RedirectStandardOutput = true;
processStartInfo.WindowStyle = ProcessWindowStyle.Hidden;
processStartInfo.CreateNoWindow = true;
Process process = new Process();
process.StartInfo = processStartInfo;
bool processStarted = process.Start();
if (processStarted)
{
StreamWriter inputWriter = process.StandardInput;
StreamReader outputReader = process.StandardOutput;
StreamReader errorReader = process.StandardError;
process.WaitForExit();
ret = process.ExitCode == 0;
}
return ret;
}
}
}