首页 > 解决方案 > 虚拟 VM 上函数 Copy.File 的奇怪行为

问题描述

我编写了以下函数,它将文件复制到计算机:

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;
}

如果我在真实计算机上运行该程序,它就可以工作。但是如果我在虚拟机中运行它,我会收到以下错误:

“不支持给定路径的格式。”

我在调试器中比较字符串,但它们是相等的。

    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

我重复一遍,文件存在,路径正确。我有管理员权限。

标签: c#

解决方案


这是一个奇怪的错误。我重新启动了所有虚拟机并且错误消失了


推荐阅读