<p>Is there a way to guarantee that a file on Windows (using the NTFS file system) will use contiguous sectors on the hard disk? In other words, the first chunk of the file will be stored in a certain sector, the second chunk of the file will be stored in the next sector, and so on.</p>

<p>I should add that I want to be able to create this file programmatically, so I'd rather not just ask the user to defrag their harddrive after creating this file. If there is a way to programmatically defrag just the file that I create, then that would be OK too.</p>

[{'answer_id': 55275, 'author': 'gabr', 'author_id': 4997, 'author_profile': 'https://Stackoverflow.com/users/4997', 'pm_score': 0, 'selected': False, 'text': "<p>I believe there's no way to achieve that. You can only defragment the file after it's been written.</p>\n"}, {'answer_id': 55289, 'author': 'Will Dean', 'author_id': 987, 'author_profile': 'https://Stackoverflow.com/users/987', 'pm_score': 4, 'selected': True, 'text': '<p>I would start here:</p>\n\n<p><a href="http://technet.microsoft.com/en-us/sysinternals/bb897428.aspx" rel="nofollow noreferrer">http://technet.microsoft.com/en-us/sysinternals/bb897428.aspx</a></p>\n\n<p>and follow Mark\'s documentation of the defrag stuff:</p>\n\n<p><a href="http://technet.microsoft.com/en-us/sysinternals/bb897427.aspx" rel="nofollow noreferrer">http://technet.microsoft.com/en-us/sysinternals/bb897427.aspx</a></p>\n'}, {'answer_id': 55292, 'author': 'Frank Krueger', 'author_id': 338, 'author_profile': 'https://Stackoverflow.com/users/338', 'pm_score': 1, 'selected': False, 'text': '<p>I know of no such guarantees.</p>\n\n<p>But also keep in mind that NTFS "files" are comprised of multiple data streams. So you are actually looking for a way to guarantee that a <strong>stream</strong> is contiguous.</p>\n'}]
['https://Stackoverflow.com/questions/55256', 'https://Stackoverflow.com', 'https://Stackoverflow.com/users/2171/']