3

Alright, so the program I'm creating is like a desktop recovery for android. Now, in order to execute the commands, for example wiping cache, the command needs to be typed out and saved to a .sh file. When I open up a program like notepad++ and type out a command (i.e. wipe cache) and save it as a unix script file (.sh), and load it into my phone, it executes without any issue. However, when I try to do this in my program using the stream writer, and saving the file as .sh, the recovery on my phone can't read it. Even though the files look exactly the same.

Okay, I know this might be a little confusing, but picture this.

On Notepad++ I create a new document, type in, wipe cache, save as Unix script (.sh), and works perfectly.

In my program

FileInfo fi2 = new FileInfo("flash.sh");
            StreamWriter writebat2 = fi2.AppendText();
            string f = "wipe cache"; 
            writebat2.WriteLine(f);
            writebat2.Close();

Now they both produce a file (flash.sh) with the exact same content. However, the one created with my program is unable to be read by the recovery. Any ideas? Thanks.

Movieboy
  • 373
  • 1
  • 3
  • 16

1 Answers1

5

There's a difference between how UNIX textfile end of line (with the single character \n), and Windows end of line (with two characters \r and \n).

So in Windows there's an extra character, and this is probably what's causing your issues when the file is transferred to your Android device.

The reason it works when you create the file in Notepad++ is that when you do save as .sh it recognizes it as a UNIX bash script file type and intelligently doesn't put the extra Windows character at the end of each line.

But the C# code you wrote DOES put the extra character since it doesn't have that type of awareness of the output file type.

You need to output your text files without that extra character, using something like the following for the write operation:

File.WriteAllText(myOutputFile, string.Join("\n", lines));

Or even easier, just use write() and add the UNIX newline yourself:

string f = "wipe cache\n";
writebat2.Write(f);
Community
  • 1
  • 1
Ryan Weir
  • 6,377
  • 5
  • 40
  • 60
  • You sir are amazing. It worked perfectly, thanks! Good explanation too, I figured it probably had something to do with the encoding or characters but wasn't too sure. – Movieboy Jul 14 '13 at 03:49