1
\$\begingroup\$

I want to get the filename and last 23 characters when I drag into a textbox.

I have it working, but I would like to just have one line. Currently, I think I am doing a bit of a workaround. As I must use a string array, and I only want one string, so I don't really need an array.

private void First_DragDrop(object sender, DragEventArgs e)
{
    string[] fileList = e.Data.GetData(DataFormats.FileDrop) as string[];
    string filename = Path.GetFileNameWithoutExtension(fileList[0]);
    First.Text = filename.Substring(filename.Length - 23, 23);
}

As you can see, I must do it in 3 steps. First, get the string array. Then, get one string from that array and turn it into a filename. FInally, I need to get the last 23 characters of that string.

I think I should be able to just get everything immediately. Though maybe it's not possible? It just seems weird that I must use an array that contains one thing.

\$\endgroup\$
2
  • \$\begingroup\$ Thanks as always Jamal, much appreciated for the corrections:) \$\endgroup\$
    – Zerowalker
    Aug 29, 2013 at 23:30
  • \$\begingroup\$ You're welcome. :-) Just want to help others help you. \$\endgroup\$
    – Jamal
    Aug 29, 2013 at 23:31

1 Answer 1

3
\$\begingroup\$

Yes, you can write everything in one line, but you will not be happy with the result. And no, .Net creators did not think of implementing a single call which would return exactly 23 characters of dropped file name. :) Silly them. :)

Meanwhile, your code makes sense. I see only two possible issues:

  1. You do not handle multiple files being dropped (that is why there is an array)
  2. You do not handle fileList being null.
\$\endgroup\$
6
  • \$\begingroup\$ Hehe, well to bad;P But the problem is, as you say, i have an array for multiple (i never drop multiple files), and which is why i want to only get the file dropped (if i drop more files, just ignore them or something), cause i am only interested in 1 file, it doesn´t make sense to get that info from many files at the same time. \$\endgroup\$
    – Zerowalker
    Aug 30, 2013 at 8:58
  • 1
    \$\begingroup\$ @Zerowalker, if you develop application for yourself - then its fine, i guess. If not, then you should not assume that user will only drop single file. You should not even assume that he will drop files, and not something else. \$\endgroup\$
    – Nikita B
    Aug 30, 2013 at 9:33
  • \$\begingroup\$ it´s for myself, so it shouldn´t be a problem:) \$\endgroup\$
    – Zerowalker
    Aug 30, 2013 at 10:31
  • \$\begingroup\$ But is there a way to simple put it into a single string? or am i forced to use an array? \$\endgroup\$
    – Zerowalker
    Aug 31, 2013 at 7:57
  • \$\begingroup\$ @Zerowalker, you have to use an array \$\endgroup\$
    – Nikita B
    Sep 2, 2013 at 5:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.