Hack Forums
[.NET] Moving A File [Snippet] - Printable Version

+- Hack Forums (https://hackforums.net)
+-- Forum: Tech (https://hackforums.net/forumdisplay.php?fid=444)
+--- Forum: Coding (https://hackforums.net/forumdisplay.php?fid=151)
+---- Forum: Software Development (https://hackforums.net/forumdisplay.php?fid=118)
+---- Thread: [.NET] Moving A File [Snippet] (/showthread.php?tid=819419)



Moving A File [Snippet] - Pulser - Nov 14, 2010

Moving it in C#


Hey, Just thought I would show you all how to move a file in C#. As I'm sure you can imagine, this can be used for many different things. You could use it for malware, or even legitimate reasons.

I'm sure someone here can come up with something!

[IMG]http://img545.imageshack.us/img545/272/37084486.gif[/IMG]

Usings
Code
using System.IO;

Variables
Please Submit File Locations For Your Own!
Code
string currentloc = "C:\\Users\\Smith\\Desktop\\test doc.txt";
    string folderloc = "C:\\Users\\Smith\\Desktop\\New folder";
    string newloc = "C:\\Users\\Smith\\Desktop\\New folder\\test doc.txt";

Moving It
Code
try
    {
    if (Directory.Exists(folderloc))
    {

    if (File.Exists(currentloc))
    {
    File.Move(currentloc, newloc);

    }

    else
    {
    MessageBox.Show("Invalid File");
    }
    }
    else
    {
    MessageBox.Show("Invalid Directory");
    }
    }

    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }

Usage:
Code
File.Move(currentloc, newloc);



* - H3x0S - Nov 14, 2010

You could've post the MSDN page instead of typing this...

http://msdn.microsoft.com/en-us/library/c6cfw35a.aspx


* - Pulser - Nov 14, 2010

(Nov 14, 2010 12:02 PM)H3x0S Wrote:  You could've post the MSDN page instead of typing this...

http://msdn.microsoft.com/en-us/library/c6cfw35a.aspx

Probably, a lot of what is posted on this forum can be found by a simple Google search. However, I would think me being here to help as well as posting a simplified way of doing it (compared to MSDN), would make it a bit easier on beginners.

Thanks for the comment. 👍


* - ident - Nov 14, 2010

(Nov 14, 2010 12:02 PM)H3x0S Wrote:  You could've post the MSDN page instead of typing this...

http://msdn.microsoft.com/en-us/library/c6cfw35a.aspx

I doubt 90% of the people on this forum even know that MSDN even exists.


* - Pulser - Nov 14, 2010

(Nov 14, 2010 12:15 PM)ident Wrote:  I doubt 90% of the people on this forum even know that MSDN even exists.

lol, thanks for the comment ident. Always glad to see people who think the same way as me.


* - Mr. Anderson - Nov 14, 2010

thanks for posting! I love your tuts and examples, they are always simple.


* - H3x0S - Nov 14, 2010

This is not a tutorial. Nothing is explain here.


* - Pulser - Nov 14, 2010

(Nov 14, 2010 12:28 PM)H3x0S Wrote:  This is not a tutoria. Nothing is explain here.

It is not a step by step tutorial, however, it does show you how to do something.

So, maybe consider it a "guide" on how to move a file.

Also, thanks for the positive comment Mr. Anderson.


* - H3x0S - Nov 14, 2010

No, it's not a guide, it's a snippet.


* - Pulser - Nov 14, 2010

(Nov 14, 2010 12:41 PM)H3x0S Wrote:  No, it's not a guide, it's a snippet.

Okay, a snippet then.

Why does it matter?


* - H3x0S - Nov 14, 2010

(Nov 14, 2010 12:43 PM)Mr. Predictable Wrote:  Okay, a snippet then.

Why does it matter?

Well, if someone wants to see a tutorial for moving a file, he expects the real tutorial with explanations. 🙂


* - Pulser - Nov 14, 2010

(Nov 14, 2010 12:46 PM)H3x0S Wrote:  Well, if someone wants to see a tutorial for moving a file, he expects the real tutorial with explanations. 🙂

Well, that makes sense. However, I never specified that it was a tutorial in the OP :D

I'm going to add in that it is just a snippet. Thanks for the comments :D