I am trying to edit the raw data of a FAT drive (I think I found a solution for NTFS, but didn't work for FAT. I don't have anything with FAT, just all my devices are using it) with C# (the result should be a drive in a different format - my own format). I was able to read the raw data (was nice seeing the FAT from inside) from it using CreateFile and opening a stream using the IntPtr I got, but couldn't write to it.
I tried several computers, USB flash drives, SD cards, floppy disks - nothing.
If it isn't possible with C#, I can do it with another language and later call the function using DLLImport.
Thanks.
If you edit/modify the drive on the sector level it could be no longer be fully compatible.
The standard way is to make a big file to fill al the space and then operate on those sectors.
Since your goal is space FAT is actually not efficient. If you control both ends ( read/write) you can just cange sector 0 so that is is not recogninzed as an existing file system and then you can wirte your own sectors.
Windows would nag you at insertion that the drive is not formatted.
Related
I've got a flash card that I need to compute a checksum on the entire contents of the drive.
If I could acquire a stream to the entire drive I could just read it in bit by bit.
Does anyone know if there is an API for doing this?
Everything I see so far requires me to open a file.
Is there any way to just read an entire drive's contents bit by bit?
If you want to write C# code, then you'll have to use P/Invoke to read data from your disk (RAW access).
Is there any way to just read an entire drive's contents bit by bit?
You'll have to make a difference between the drive (logical representation of your flash card, with a FileSystem installed on it, specified by the drive letter) and the disk (physical representation of your flash card, specified by the disk number).
See my previous answer about how to read RAW data from a drive/disk:
Basically, you'll first need a handle to the disk/drive:
// For a DISK:
IntPtr hDisk = CreateFile(string.Format("\\\\.\\PhysicalDrive{0}", diskNumber),
GenericRead,
Read | Write,
0,
OpenExisting,
0,
IntPtr.Zero);
// For a DRIVE
IntPtr hDrive = NativeMethods.CreateFile(
string.Format("\\\\.\\{0}:", DriveLetter)
GenericRead,
Read | Write,
IntPtr.Zero,
OpenExisting,
0,
IntPtr.Zero);
Then use SetFilePointerEx (so you can move the offset where you want to read), ReadFile (fills a buffer with bytes read from the disk/drive), CloseHandle (closes the handle opened by CreateFile).
Read the disk/drive by chunks (so basically, a loop from offset "0" to offset "disk/drive size").
What's important (or ReadFile will always fail): the size of read chunks must be a multiple of the sector size of your disk (512 bytes generally).
I'm not sure if there is direct support for this in .NET, but you can use Platform Invoke to call the Win32 API functions. CreateFile() should be your starting point, as it allows you to get a handle to the physical drive:
You can use the CreateFile function to open a physical disk drive or a volume, which
returns a direct access storage device (DASD) handle that can be used with the
DeviceIoControl function. This enables you to access the disk or volume directly, for
example such disk metadata as the partition table.
The documentation informs you of restrictions (such as requiring administrative privileges) and hints at how to get a physical drive number from the volume letter, etc.
"Stream" is an abstraction that normally assumes the presence of a file system, so conceptually it's not quite as simple as that.
On Windows, you might want to start by looking at the Windows Defragmentation API. Here are a couple of links I found:
MSDN page
Defrag API C# wrappers
This is a bit of a weird question but, with the functionalities of C++, c# and objective C as we speak is there any possible way for video content to be uploaded whilst its recording. So as you record the video it would be being compressed and uploaded to a website.
Would this involve cutting the video into small parts as you record, hardly noticeable stops and starts during the recording?
If anyone knows if this is at all possible, please let me know.
Sorry for the odd question.
You've just asked for streaming media -- something that's been done for over a decade (and, if you overlook "television", something that's probably been underway in research settings for several decades).
Typically, the video recorder will feed the raw data through filters of some sort -- correct white balance, sharpen or soften the video, image stabilize, and then compress the raw data using a codec. Most codec designs will happily take a block of input, work on it, and then produce a block of encoded data ready for writing. Instead of writing to disk, you could "write" to a socket opened to a remote machine.
Or, if you're working with an API that only writes to disk, you could easily re-read the data off disk as it is being written and send the data to a remote site. You'd have to "follow" the writing using something like tail -f's magic ability to follow the file as it is written. (Heck, if you're just bodging something together for a one-off, I'd even recommend using tail -f as part of your system.)
It depends on if the application recording to disk is locking the file. My guess is that, unless you wrote the recording software, the application locks the file(or doesn't even create the real file) until it stops recording. If you are writing the recording software as well, then yes, you can do this. you would just use sychronized threads.
I need to transform/convert/process TransXchange data dump to reduce the size of data as some of the xml files can be up to 400 MB. I have the following options:
Sqlite database
CSV files
Binary Serialization
?
What is the best method of reducing the file sizes so that they would be feasible for use in a Windows Phone 7 application?
EDIT: I am going to create journey planning application that will allow users to specify source and destination. The application will then present the available services. In down under we have spotty mobile broadband coverage therefore I am aiming to have offline application.
This analysis is superb for showing you the timing of serialisation: http://www.eugenedotnet.com/2010/12/windows-phone-7-serialization-comparison/
For size... It's quite easy to guess that binary is smaller than sqlite (or Sterling) which in turn is smaller than CSV
However, if you are looking at processing 400MB of data on the phone... then I'd say you are doing the wrong thing - farm the processing out to a server (to the cloud?) and just view the summary results on the phone - think "thin client".
(Off to wash my mouth out now after all those jargon words!)
The main question is what are you going to do with that data.
If you just need to store the data and files are fine then binary serialization + compression (zlib, lzo...) will work best.
CSV won't do you any good.. will probably occupy more than the XML.
Database (for example, Sqlite) is the most expensive it terms of storage but you can manage and search the data more easily.
Given the path of a string i want to wipe out the contents of a file. The natural way I thought (which maybe incorrect) was to open a FileStream to the file and write gibberish (random data perhaps taken from a RNGCryptoServiceProvider) to it. And then perhaps do this several times and then delete the file.
My problem is that while this may look logically correct, i read up on another blog that Windows might actually choose to write the file to a different place in the hard disk.
Is that the case in Windows Mobile? Will this actually be a problem? Does this writing to a different location in the hard disk apply to even flash based (SD etc) cards ?
I've not personally done this, but you will probably need to use the low-level FLASH driver IOCTLs to do this correctly.
http://msdn.microsoft.com/en-us/library/aa927166.aspx
I think IOCTL_FMD_RAW_WRITE_BLOCKS looks particularly useful.
-PaulH
Another possibility that may work would be to erase the file normally, then use the defragment APIs to wipe ALL of the freespace on your flash. Since you're wiping everything, it won't be necessary to know exactly where on the disk your file was. But, this will wear out your flash drive more quickly. The C# method is detailed in this blog post: http://blogs.msdn.com/b/jeffrey_wall/archive/2004/09/13/229137.aspx
I'm using C# & the compact framework on an embedded device to log data to a compact flash card. Because data integrity is of upper most importance, I'm wondering how to write the data to the flash disk. Will files get lost/damaged if power is lost during a write/flush or while the file is opened? What's the best way to do this?
By the way, the card uses FAT32 as file system if that's important.
greetings,
Korexio
If performance is not an issue, I personally would prefer to use the first method which is OPEN-WRITE-CLOSE. FAT32 is really vulnerable to data damage during write operation.