Note that there are some explanatory texts on larger screens.

plurals
  1. POSaving a file that has been opened by a particular process in C#
    text
    copied!<p>I'm working with a filetype that doesn't exist in the public domain. The file is in binary or hex, but there's been a previous batch file written that runs when you double click on a file in order to open it as a .txt file in notepad. </p> <p>What I am trying to do is to open this file within my program, and then save it while it is open so that it is now a .txt file, and I can work with it.</p> <p>So I start the batch file process and the file itself is the argument.</p> <pre><code>/*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(); </code></pre> <p>I then look to see if any of the current running processes have the file extension and are running in notepad, using Regex:</p> <pre><code>/*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 &gt; 0) { useMatch = matches.Count; for (int i = 0; i &lt; 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()); } } } } </code></pre> <p>I've been Googling all day - I must be going wrong somewhere in my approach, but I didn't think it should be so difficult to figure this out. Any help would be much appreciated - I'm not native to C# either, so excuse any bad coding practices. </p> <p>Can anybody help me to figure out how to change this file to a .txt file? Also, C# obviously doesn't read it as .txt so I can't just change the file extension as in Notepad, which is why I'm trying to do it this way. I also spent a lot of time messing around with encoding but to no avail.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload