2013-05-29 37 views
1

我正在處理公用域中不存在的文件類型。該文件是二進制文件或十六進制文件,但是當您雙擊某個文件以便將其作爲記事本中的.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,所以我不能像在記事本中那樣更改文件擴展名,這就是爲什麼我要這樣做。我也花了很多時間搞亂編碼,但無濟於事。

+0

什麼是您的批處理文件嗎? –

+0

嗨,我沒有自己編寫批處理文件(我從來沒有寫過),但基本上它是當您嘗試打開其中一個文件時,它找到該文件的最新版本並啓動記事本爲了閱讀它 - 還有另一批文件將其轉換爲可讀形式(我認爲) - 這就是我所知道的,道歉! – user1261648

+0

我會建議您修改批處理文件,以便不啓動記事本,而是對其執行其他操作。如果你在文章中包含批處理文件,也許有人可以幫助你。 –

回答

0

如果是二進制的,你可以把它轉換爲文本這種方式,並將其保存:

string myString; 
using (FileStream fs = new FileStream(YourBinaryFile, FileMode.Open)) 
using (BinaryReader br = new BinaryReader(fs)) 
{ 
    byte[] bin = br.ReadBytes(Convert.ToInt32(fs.Length)); 
    myString = Convert.ToBase64String(bin); 
} 

然後,你可以這樣說寫入文件:

File.WriteAllText(YourTargetTxtFile, myString); 

或用它作爲你看適合。 希望有所幫助!

編輯:

您可以使用此功能爲好,過載指定編碼:

File.WriteAllText(file, contents, encoding); 
+0

克里斯 - 我已經試過(廣泛:)),它不工作;我認爲它是ANSI的事實是那裏問題的一部分。基本上,它可以保存它的所有權利,但它編碼在gobbledygook中。所以我也不能使用它。因此,我爲什麼要嘗試一種非常複雜的方式! 感謝您嘗試,但非常感謝。 – user1261648

+0

哦,我現在明白了。我以爲你做得非常複雜 - 但也許你不應該試圖發明輪子。你有沒有試過指定編碼?更新我的答案。 – Chris

+0

嗨,是的,我仍然沒有運氣!仍在創建的txt文件中的隨機字符。如何做到這一點我真的很茫然;它不應該那麼困難。也許它與原始文件類型有關 - 正如我所說的那樣,它是一個私有擴展,所以這可能是編碼被破壞的原因! – user1261648