6

I am working on a Windows Forms Application.

During one Drag and Drop action on a TextBox control, I want to restrict the user to provide only a text file.

// drag drop module for input text file in textbox starts here
private void textBoxInputTextFile_DragEnter(object sender, DragEventArgs e)
{
    if (e.Data.GetDataPresent(DataFormats.FileDrop))
        e.Effect = DragDropEffects.Copy;
    else
        e.Effect = DragDropEffects.None;
}

private void textBoxInputTextFile_DragDrop(object sender, DragEventArgs e)
{
    if(e.Data.GetData(DataFormats.FileDrop, true))
    {
    // Check if it is a text file
    // Okay if it is a text file or else give an error message
    }
}

This code is just a sample from my previous folder drop action but now I want to restrict it to only one file and that too must be a text file. So that, when the drop action happens, it should check first if it is a text file or not and then do other stuff.

How do I do that?

Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Indigo
  • 2,887
  • 11
  • 52
  • 83
  • 1
    What do you mean "a text file"? with ".txt" suffix? maybe also csv or rft? – Amiram Korach Nov 01 '12 at 10:29
  • @Amiram Korach: Hi, I mean *.txt file only. Thanks – Indigo Nov 01 '12 at 10:31
  • Or .log, .config, .xml - all are text files. – Kevin Nov 01 '12 at 10:31
  • Sorry for the confusion, I meant only for a '*.txt' file, no other format, at all. – Indigo Nov 01 '12 at 10:32
  • http://www.codeproject.com/Articles/19941/Drag-and-Drop-text-containing-file-in-textbox – opewix Nov 01 '12 at 10:33
  • @JesseJames: thanks, It seems that this will read the file. Reading file is not a problem at all. The question is how can we restrict the user to drop only a '*.txt' file and nothing else. I mean, it must understand that the dropped object is a '*.txt' file and then proceed to read otherwise give a message to provide a proper '*.txt' file. I want my application to have a complete control on it. – Indigo Nov 01 '12 at 10:41

1 Answers1

8

Written off the top of my head (untested):

var files = (string[])e.Data.GetData(DataFormats.FileDrop);

foreach(var file in files)
{
    if(System.IO.Path.GetExtension(file).Equals(".txt", StringComparison.InvariantCultureIgnoreCase))
    {
        //file has correct extension, do something with file
    }
    else
    {
        MessageBox.Show("Not a text file");
    }
}

Before putting this sort of thing into production, I'd probably add more null checks (what if a file doesn't have an extension for example?) but this should give you the basic idea.

If you want some kind of more stringent test to see if the file being dropped is a text file rather than just check it's extension, I'd recommend reading this SO question.

Community
  • 1
  • 1
Bridge
  • 29,818
  • 9
  • 60
  • 82
  • wow, it worked fine. Thanks a lot, a few minutes before I tried a similar approach but it didn't work. I was trying to get only one file from it. This is wise to create an array and check it later. Perfect :) – Indigo Nov 01 '12 at 10:45
  • really thanks, much helping in understanding it well. I will go through your suggestions. I will post my final code once it is done and I apply all checks on it, so that someone can use it later. Thanks again. – Indigo Nov 01 '12 at 10:49
  • 1
    @Chetan Despite what I said about null checks, `Path.GetExtension()` shouldn't return null in your case - it returns `string.Empty` if there is no extension, but will only return null if somehow the file path is null (which shouldn't happen here). Documentation on it is [here](http://msdn.microsoft.com/en-us/library/system.io.path.getextension.aspx). – Bridge Nov 01 '12 at 10:50
  • I will pay attention to this in my code. This is really important while learning programming even in general. Thanks again for such a nice explanation and pointing out the possibilities :-) – Indigo Nov 01 '12 at 11:04