我正在處理公用域中不存在的文件類型。該文件是二進制文件或十六進制文件,但是當您雙擊某個文件以便將其作爲記事本中的.txt文件打開時,會出現一個先前編寫的批處理文件。保存已被C#中特定進程打開的文件
我想要做的是在我的程序中打開這個文件,然後在打開時保存它,以便它現在成爲一個.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,所以我不能像在記事本中那樣更改文件擴展名,這就是爲什麼我要這樣做。我也花了很多時間搞亂編碼,但無濟於事。
什麼是您的批處理文件嗎? –
嗨,我沒有自己編寫批處理文件(我從來沒有寫過),但基本上它是當您嘗試打開其中一個文件時,它找到該文件的最新版本並啓動記事本爲了閱讀它 - 還有另一批文件將其轉換爲可讀形式(我認爲) - 這就是我所知道的,道歉! – user1261648
我會建議您修改批處理文件,以便不啓動記事本,而是對其執行其他操作。如果你在文章中包含批處理文件,也許有人可以幫助你。 –