c# - 虚拟 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
我重复一遍,文件存在,路径正确。我有管理员权限。
解决方案
这是一个奇怪的错误。我重新启动了所有虚拟机并且错误消失了
推荐阅读
- recursion - 从 prolog 中的直接映射缓存中获取数据
- google-apps-script - 如何找到我的哪些谷歌工作表脚本有任何定时触发器
- image - Android Image Cropper 不适用于相机中的图像
- r - 按 ID 和时间戳合并两个大型数据集,并在 R 中获取下 n 个时间间隔的测量值
- kubernetes - Prometheus 指标配置
- amazon-web-services - 我有一个角色可以完全访问 SQS,但我仍然无法向 SQS 发送消息
- javascript - 从字符串创建嵌套数组
- javascript - 例如,当我选择明矾时,它会存储它,然后当我选择温和金属时,它会显示两个值(在完成所有计算之后)
- python - 使用 Python/Selenium 抓取文本并将 CSS 格式转换为 Markdown
- unit-testing - 如何在嵌套 HttpService 上用玩笑测试 retryWhen