Strange behavior with function Copy.File on virtual VM

-1

I wrote the following function, which copies file(s) to computers:

public void CopyFile()
{
    //data
    _pathcopy = txtb_to.Text;
    _computers = txtb_pc.Lines;
    _copyfiles = txtb_from.Text;

    string fileName = GetFileNameFromLabel(_copyfiles);
    string from = Path.Combine(_copyfiles);
    foreach (var comp in _computers)
    {
        string topath = GetUNCPath(_pathcopy, comp, fileName);
        Directory.CreateDirectory(GetUNCPath(_pathcopy, comp));
        try
        {
            File.Copy(from, topath);
        }
        catch (Exception e)
        {
            MessageBox.Show(e.Message);

        }
    }
}

private string GetFileNameFromLabel(string path)
{
    string fName = Path.GetFileName(path);

    return fName;
}

private string GetUNCPath(string path, string pc)
{
    string ffp = Path.GetFullPath(Path.Combine(path + @"\"));
    string uncpath = @"\\" + pc + @"\" + ffp.Replace(":", "$");
    return uncpath;
}

private string GetUNCPath(string path, string pc, string fln)
{
    string ffp = Path.GetFullPath(Path.Combine(path + @"\" + fln));
    string uncpath = @"\\" + pc + @"\" + ffp.Replace(":", "$");
    return uncpath;
}

It works if I run the program on a real computer. But if I run it in a VM, I get the following error:

"The given path's format is not supported."

I compare strings in debugger, but they are equal.

    System.NotSupportedException
      HResult=0x80131515
      Message=The given path's format is not supported.
      Source=mscorlib
      StackTrace:
       at         

 System.Security.Permissions.FileIOPermission.EmulateFileIOPermissionChecks(String fullPath)
   at System.Security.Permissions.FileIOPermission.QuickDemand(FileIOPermissionAccess access, String fullPath, Boolean checkForDuplicates, Boolean needFullPath)
   at System.IO.File.InternalCopy(String sourceFileName, String destFileName, Boolean overwrite, Boolean checkHost)
   at System.IO.File.Copy(String sourceFileName, String destFileName)
   at CopyInstall.Form1.CopyFile() in \\dc1\temp\CopyInstall\CopyInstall\Form1.cs:line 47
   at CopyInstall.Form1.bt_Copy_Click(Object sender, EventArgs e) in \\dc1\temp\CopyInstall\CopyInstall\Form1.cs:line 28
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
   at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
   at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
   at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(IntPtr dwComponentID, Int32 reason, Int32 pvLoopData)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
   at System.Windows.Forms.Application.Run(Form mainForm)
   at CopyInstall.Program.Main() in \\dc1\temp\CopyInstall\CopyInstall\Program.cs:line 19

I repeat, file exist, path correct. I have admin rights.

c#
asked on Stack Overflow Nov 27, 2018 by Alex • edited Nov 27, 2018 by Alex

2 Answers

1

It was strange bug. I rebooted all VM and bug is gone

answered on Stack Overflow Nov 29, 2018 by Alex
0

Try to remove last "\" from the destination location.

private string GetUNCPath(string path, string pc)
{
    string ffp = Path.GetFullPath(Path.Combine(path);  // + @"\"));
    string uncpath = @"\\" + pc + @"\" + ffp.Replace(":", "$");
    return uncpath;
}

If this doesn't work, then you have permission issues. Maybe from VM you have no permission to write on remote folder.

answered on Stack Overflow Nov 27, 2018 by Obelixx • edited Nov 27, 2018 by Obelixx

User contributions licensed under CC BY-SA 3.0