Read from file to find file path. C#

Pho3nix

The Legend
Joined
Jul 31, 2009
Messages
32,847
Reaction score
3,041
Location
On the toilet
Hi guys,

Need to configure a Windows app to :
  1. Split a file into chunks and put it into a folder
  2. Save the chunks in 10mb portions
  3. create a filetype of it's own, housing the folder path of the folder where the split file parts reside
  4. Merge the parts and create the path

Now the first,2nd and last part I have done. The 3rd thing is what is a tad confusing, what would I need to do to create a file on save that makes a file, only readable by the application that houses the folder path. Now when I click on said file, it opens the app up and starts merging the parts.

Thoughts?
 
Well firstly, to open a file with an application by clicking on it, you need to register that filetype (the extension rather) with a specific application on your computer. I can't remember how to do this. When you "install" this app you should be able to configure that.

Secondly, you're obviously going to want to make it a binary file. Hey, it just looks cooler than a straight-up ascii file.
 
Code:
            string SaveDOBFile = Application.StartupPath;
            SaveDOBFile = Path.Combine(SaveDOBFile, "FileShortcuts");
            SaveDOBFile = Path.Combine(SaveDOBFile, strFileName);
            using (FileStream testStream = File.Open(SaveDOBFile, FileMode.Create))
            using (BinaryWriter bwrite = new BinaryWriter(testStream))
            {
                bwrite.Write(txtSaveTo.Text);
            }

Figured out how to register the extention with the application. This is my code above.. Thats my code above for creating the binary file. My only prooblem is that in my thinking, I have the Folder set but I never set the filename so it would be
Code:
using (FileStream testStream = File.Open(SaveDOBFile, FileMode.Create))

but from my examples, there must always be a filename so :

Code:
using (FileStream testStream = File.Open("C:\test", FileMode.Create))

Now I cant get the "\" in there so it's missing the filename. Tried "
Code:
SaveDOBFile = SaveDOBFile + ("\" + strFileName);
but this doesnt work :(
 
lol ok, :o.. wow feel so stupid :o

Now, how would I go about setting what the app should do when I double click on the file?
 
Well you are always going to have to have a reference to a file path (or at least the first file in the series) if you would have more than one set of split files in a directory.

If each series will always be in its own folder, you can save the directory path, and use the DirectoryInfo's GetFiles() method.

I'd recommend one of two things:

1. Name each file with a part number... like "MyAwesomeLargefile_1.yourPath", "MyAwesomeLargefile_2.yourPath" etc. You can then use IndexOf('_') and SubString the file names to get the sequence.

2. The second would be a little more reliable... when you write the file, append a few bytes to the beginning of the file with a serial number for the files series, and a byte for the file part. This way you can always find which files belong together and in which order by reading the first few bytes of the files. This way if they get renamed by a user... no big deal. Like this:

int fileSeriesSerialNumber = 1234;
byte fileSequence = 1;

// for each file in the sequence fileSequence +=1
// Stuff here that breaks the file apart
using (BinaryWriter bwrite = new BinaryWriter(testStream))
{
bwrite.Write(fileSeriesSerialNumber);
bwrite.Write(fileSequence);
bwrite.Write(txtSaveTo.Text);
}

I'd also look at a gzipStream for compression while you are at it. (http://msdn.microsoft.com/en-us/library/system.io.compression.gzipstream.aspx) If the default implementation takes up too much memory due to large files (the default implementation loads the while file into memory), there are examples on the net where you can chunk the compression... I've done that before.
 
Using threading to get around the memory issue.. Will look at gzipStream aswell tho.
Now I have my binary file which has the file path of the split files. How do I go about setting the application to understand that when the file is opened it should insert the file path into a text box? Thoughts?
 
Yep, we sorted the splittin but got given more criteria. Will look into Command line arguments.. anything else I should look at?
 
Yep, we sorted the splittin but got given more criteria. Will look into Command line arguments.. anything else I should look at?

Hmm... just a question... what's the difference between your application, and hjsplit?
 
Code:
        private void btnUpload_Click_1(object sender, EventArgs e)
        {
            File.Copy(strFileName, strUploadTo);

            string SaveDOBFile = Application.StartupPath;
            SaveDOBFile = Path.Combine(SaveDOBFile, (strName+".bob"));
            //SaveDOBFile = SaveDOBFile + @"\" + strFileName;
            using (FileStream testStream = File.Open(SaveDOBFile, FileMode.Create))
            using (BinaryWriter bwrite = new BinaryWriter(testStream))
            {
                bwrite.Write(strUploadTo);
            }
            MessageBox.Show("File has been successfully copied over");

File Stream isnt creating the file for some reason :\
 
Last edited:
Top
Sign up to the MyBroadband newsletter
X