File.Move on client machine Asp.net MVC

1.8k Views Asked by At

Silly question but here goes...

Is it possible to write an intranet windows auth asp.net mvc app that uses File.Move to rename a file on a users machine? Or will the File.Move and using Path.GetDirectory and other System.IO functions look on the IIS server directory structure instead of the client machine?

    [HttpPost]
    public ActionResult Index(HttpPostedFileBase file, string append)
    {
        try
        {
            if (file != null && file.ContentLength > 0)
            {
                var fileName = Path.GetFileName(file.FileName);
                DirectoryInfo filepath = new DirectoryInfo(file.FileName);
                string parentpath = Path.GetDirectoryName(filepath.FullName);
                DirectoryInfo searchablePath = new DirectoryInfo(parentpath);

                var directories = searchablePath.GetFiles("*", SearchOption.AllDirectories);

                foreach (FileInfo d in directories)
                {
                    if (!string.IsNullOrEmpty(append) && !d.Name.Contains(append))
                    {
                        string fName = Path.GetFileNameWithoutExtension(d.Name);
                        string fExt = Path.GetExtension(d.Name);

                        System.IO.File.Move(d.FullName, Path.Combine(d.DirectoryName, fName + append + fExt));
                    }
                }
            }
        }
        catch (Exception ex)
        {

        }

        return View();
    }

I have tried this but am getting a filenotfoundexception.

Any ideas?

2

There are 2 best solutions below

0
On

These will indeed work only on the server.

You may look at the various file and filesystem related specifications for client-side javascript APIs provided by the user's browser:

0
On

The ASP.NET code runs on the server, so it will look at the files on the server.

You can't rename a file on the client machine, however it would be possible to rename a file on the computer that is used as client, if:

  • the server and computer are on the same network
  • the server knows the name of the computer
  • the server knows which folder to look for in the computer
  • the folder is shared with the user account running the ASP.NET code on the server with enough privileges to change the name of a file

In that sense the computer is not a client to the server, but the server communicates directly with the computer via the file system, not via IIS.