Cygwin Commands from Web Service ASP.NET C# - c#

I want to execute a cygwin command from within a webservice.
Basically I want to use the "tail" command to strip off the first line of a file in C#.

Calling another program just to strip the first line of a file sounds like a very bad idea. You might want to try and just strip the first line in C#.

I've not personally dealt with huge text files before, so I did a bit of searching around;
Efficient way to delete a line from a text file
Basically, this one gives an answer you don't like, but if .NET 4 is an option memory-mapped files might help you out.

Are you looking to remove it or read it? If you want the first line of the file, you can just open the file stream (File.Open) and take the first line.

Normally Cygwin is installed in C:\CYGWIN so you should be able to run tail (from /usr/bin) by calling "C:\cygwin\usr\bin\tail.exe" from your code.
That said, you really should not be doing this at all. Just use a StreamReader properly. This question has a nice example to show how: Reading large text files with streams in C#

Related

Remembering the last position in a .txt file in C#?

I found myself being stuck on a certain problem. Currently I am writing a program in C#, that reads in .txts. These .txts are constantly changing, as in content is being added to them.
I created a FileSystemWatcher, that hands the name of the file to the method reading in my txt. Now I would like to have something in between, that remembers the last position I read in the txt and then only passes on the new part.
It also needs to remember it the next time I start the program.
I thought about storing the last line in a xml or txt and then letting my method search that file for the last line noted there.
The only similar question I found was this: Read log file from last read position . I don't really understand it though.
Is there anyway how to do this more efficient or elegant?
Edit: I already linked the suggested question and no, it is not similar to mine. I am looking to permanently store the information on how far I am into a .txt, not just on runtime.
When you need to store a finite amount of information on program state on the Windows platform in between runtimes, the Windows Registry is usually the way to go. Here's a stackoverflow question that will show you how. Use HKEY_CURRENT_USER so that you don't have UAC/Admin Rights issues:
Writing to registry in a C# application
.NET 4.0
Will this help? One way could be to store the line number instead of storing the content of the last line read.
Since .NET 4.0, it is possible to access a single line of a file directly. For instance, to access line 15:
string line = File.ReadLines(FileName).Skip(14).Take(1).First();
This will return only the line required

How to merge 2 zip files together into 1 zip

I am trying to make a custom launcher for Minecraft in C# but I have come across a bump.
I want to add something into it, Minecraft Forge, but the only way I could think of is to change the extension of minecraft.jar to minecraft.zip, extract the contents of the Minecraft Forge.zip and the minecraft.zip into the same folder and then zip that entire folder up into minecraft.jar.
However minecraft.jar has a file named aux.class so whenever my extract script (Made in java) tries to extract it, it simply says:
Unable to find file G:\Programming\C#\Console\Forge Installer\Forge Installer\bin\Debug\Merge\aux.class.
The only other way I can think of is to merge minecraft_forge.zip into minecraft.zip, I have spent around 2 hours looking on Google (watch as someone sees it within a couple of minutes) but it always shows me results for "How to zip multiple files", "How to make a zip file in C#" etc.
So I have come here looking for my answer, sorry if this is a lot to read but I always see comments on here saying "You didn't give enough information for us to help you with".
EDIT: The question in case it wasn't clear is: How am I able to put the contents of minecraft_forge.zip into minecraft.zip?
In your case, if you cannot unzip the files due to OS limitations, you need to "skip" unzipping temporary files to zip them. Instead, only handle input & output streams, as suggested in the answers found here: How can I add entries to an existing zip file in Java?
As you pointed out, "aux" is a protected keyword within windows and it does not matter what the file suffix may be; windows won't let you use it. Here are a couple of threads that discusses this in general.
Ref 1: Windows reserved words.
Ref 2: Windows reserved words.
If you are typing in commands to perform the copy or unzip, there is a chance you can get this to work by using a path prefix of the following \\.\ or \\?\. When I tested this, it worked with either a single or double back-slash following the period or question mark. Such that the following work:
\\.\c:\paths\etc
\\.\\c:\paths\etc
\\?\c:\path\etc
\\?\\c:\path\etc
I used the following command to test this. When trying to rename through windows explorer it gave a "The specified device name is invalid." error message. From the command line it worked just fine. I should point out, that once you create these files, you will have to manually delete them using the same technique. Windows Explorer reports that these text files which have a size of 0 bytes "is too large for the destination file system", ie... the recycle bin.
rename "\.\c:\temp\New Text Document.txt" aux.txt
del "\.\c:\temp\aux.txt"
As far as copying directly from zip or jar files, I tried this myself and it appeared to work. I used 7-zip and opened the jars directly using the "open archive..." windows explorer context menu. I then dragged-and-dropped the contents from forge.jar to the minecraft jar file. Since it is the minecraft jar file with the offending file name the chance of needing to create a temporary file on the filesystem is reduced. I did see someone mention that 7-zip may extract to a temporary file when copying between jars and zips.
7-zip reference on copying between archives
I should point out that my copy of minecraft jar (minecraft_server.1.8.7.jar) did not contain a file named aux.class. I also did not try to use the jar after the copy/merge. Nor did I spend too much time trying to figure out how well it merged the two contents since it appears like there may be a conflict with com\google\common\base\ since there are similar class name but with different $ variable suffixes on them.
I hope these two possible suggestions could give you some room to work with to find a solution for your needs... if you're still looking.

creating and signing a custom file type

I believe this is going to be a big question but I searched the web for at least an hour for an answer and I hope here I'll get the answer.
So the thing is I want to create my own file extension that will essentially be a binary file that my program will obviously will know how to interpret.
I know how to create a binary file and read from it, I understood that all I need is to change the extension and check it before I start reading the file.
The question is even if I create my own extension and check for it before start reading, I can't know that the file was really created by my application, someone could just change the extension and my application won't get the data it needs (of course I'll put everything in a try and catch so the application won't crush).
Is there a way to sign a binary file before opening it so people that get the file can know that my application created that file and not something that someone changed to look like my file type?
It's more of an idea I have so I don't have any code to show or any example other than this scenario.
All I need is something that people will know came from me and not something that every other person can generate on his own (not talking about programmers, I'm talking about regular people).
If you know anything about this subject please direct me to the answer or if you know post it here.
One way could be to encrypt the contents of the file, then have your application decrypt it.
Anyone opening the file in your application would immediately know if that application generated it.
I would create a special sequence at the beginning of the file (a file header)

Read a specific line in a file

I know this has been answered several times before. But I am asking this for C#. Whereas there were a few for java. There was another one for C#, but it got me nowhere. So please do not rate poorly as I have a reason for this. Thank you.
My ultimate goal is to have a settings feature in my application. And I want all these settings to be saved into a text file. And when the application opens, it will automatically read that file and adjust the controls on my form.
I am unsure of where to start, but I was wondering if there was something along the lines of
String readLine(File file, int lineNumber)
Thank you in advance.
I already have a file being saved, and a file being opened. But I only have one setting saved in there. And that takes the first line. But if I want to add more controls being saved, rather than making a new file per option, I'd like to place several lines in that file, and each line would have its own control that it would change.
So, how can I read a specific line of a text file?
Thanks once again.
Principally one must read each line of a text file to locate a specific line (stopping at that line if desired) because each text line can be of a different length, so you can't just compute an offset in the file and go there.
If this really is a configuration file (which presumably isn't huge, you could just use
string[] lines = File.ReadAllLines(myPath);
http://msdn.microsoft.com/en-us/library/s2tte0y1.aspx
That way all of the lines of the file are read into lines and you can access an individual line like
string line = lines[2];
If you really only want to read a specific line (keep in mind this will be very inefficient if you read multiple lines over time in your app because you keep re-reading from the start of the file), you will have to write your own helper routine.
If you want a settings feature use the default and don't roll your own. You will only run into more issues with parsing and upgrade and finding a place to save it so that it can be modified when users are running under UAC or non admin accounts
Unless all the lines are exactly the same length, you can't just skip to a certain line number in the file. If you'll be needing most of the options, just read the file as an array of lines using System.IO.File.ReadAllLines:
string[] lines = File.ReadAllLines("myfile.txt");
Then you can just access each line like you would normally, say lines[1] for the second line.
Also, if you don't need to modify these settings with anything else, just use the built-in settings. They handle everything for you.

Saving and reading from specific parts of a .txt file

Aspiring programmer here!
You can skip this part if you find it irrelevant.
Im currently working on a project where i give every program a username and i let people edit their own part of of each file. This will work more or less like onenote (great program), but i want to be able to restrict access to certian parts of it. I also need the file to be stored locally (on a shared location that is). In time i also want the files to be encrypted, but ill stick with the editing part of my question for now.
So my real problem:
I need people to be able to edit their part of a .txt file. The setup will probably look like this:
Document name:
Task 1: (Task name, like "Write a paragraph about Einstein"
User 1: (User 1s answer)
User 2: (User 2s answer and etc.)
Then ill follow up with more tasks / users. What i need to make my program understand is to read-only document name and other users answer, and read-write on their own part of the file.
What code can i use to accomplish this?
You won't be able to do that in a text file. You'll have to spend a little bit more effort in solving that task. Use a database and manage those tasks in a table and the user responses in another table. That will shurely work much better, trust me.
If you really want to use a text based solution for this instead of a database I would look into XML files.
They give you the ability to structure a document and have capabilities of being queried by C# trough XPath or Linq to XML queries.
This documentation should get you started.
Yea a database (use an MSAccess MDB file if you don't have a SQL server) would be best for this.
However, if you really want to use a text file, what you do is
1. Use System.IO to read the file and parse the file to get the piece of data you want them to edit (only read it out of the file, don't modify the file at this point).
2. Display the piece of data to them in a textbox or whatever.
3. After they've mdofied it, then read in the whole file into a variable
4. replace the old piece of data with the new data. in the variable.
5. output the entire variable back into the file and save and close it.
If this is REALLY what you want to do, i can post some code for you.

Categories