パブリック ドメインに存在しないファイル タイプを使用しています。ファイルはバイナリまたは 16 進数ですが、メモ帳で .txt ファイルとして開くためにファイルをダブルクリックすると実行される以前のバッチ ファイルが書き込まれています。
私がやろうとしているのは、プログラム内でこのファイルを開き、開いている間に保存して、.txt ファイルにして作業できるようにすることです。
そこで、バッチ ファイル プロセスを開始し、ファイル自体が引数になります。
/*Start the batch file and open file (as an argument)
* Then it'll be viewable as a text file
* */
Process process = new Process();
process.StartInfo.FileName = "C:/batchfile.bat";
process.StartInfo.Arguments = "C:/file.ext";
process.StartInfo.UseShellExecute = false;
process.StartInfo.RedirectStandardOutput = true;
process.Start();
次に、現在実行中のプロセスにファイル拡張子があり、正規表現を使用してメモ帳で実行されているかどうかを確認します。
/*Using a regex pattern, we can see if a wtd file has been opened
* If there is, it's added to an array called matchArr
* */
String pattern = "^.*ext.*Notepad.*$";
Regex rgx = new Regex(pattern, RegexOptions.IgnoreCase);
Process[] processes = Process.GetProcesses();
int useMatch = 0;
String[] matchArr = new String[100];
foreach (var proc in processes)
{
MatchCollection matches = rgx.Matches(proc.MainWindowTitle);
if (!string.IsNullOrEmpty(proc.MainWindowTitle))
if (matches.Count > 0)
{
useMatch = matches.Count;
for (int i = 0; i < useMatch; i++)
{
matchArr[i] = proc.MainWindowTitle;
String blah = proc.Modules[0].FileName;
string path = System.IO.Path.GetFullPath(proc.MainWindowTitle);
Console.WriteLine("Path:" +path);
StreamReader stream = new StreamReader(blah);
FileStream fstr = new FileStream("C:/newName.txt", FileMode.Create, FileAccess.Write);
using (StreamWriter strw = new StreamWriter(fstr))
{
strw.WriteLine(stream);
Console.WriteLine("Array is : " + matchArr[i].ToString());
}
}
}
}
私は一日中グーグルをしてきました-私のアプローチのどこかで間違っているに違いありませんが、これを理解するのがそれほど難しいとは思いませんでした。どんな助けでも大歓迎です-私はC#もネイティブではないので、悪いコーディング慣行を許してください。
このファイルを .txt ファイルに変更する方法を教えてくれる人はいますか? また、C#は明らかにそれを.txtとして読み取らないため、メモ帳のようにファイル拡張子を変更することはできません。そのため、このようにしようとしています。また、エンコーディングをいじるのに多くの時間を費やしましたが、役に立ちませんでした。