我编写了以下函数,该函数将文件复制到计算机:
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;
}
如果我在真实计算机上运行该程序,它将起作用。但是,如果我在VM中运行它,则会出现以下错误:
“不支持给定路径的格式。”
我在调试器中比较字符串,但它们相等。
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
我重复一次,文件存在,路径正确。我拥有管理员权限。
答案 0 :(得分:1)
这是一个奇怪的错误。我重新启动了所有虚拟机,但错误消失了
答案 1 :(得分:0)
尝试从目标位置删除最后一个“ \”。
private string GetUNCPath(string path, string pc)
{
string ffp = Path.GetFullPath(Path.Combine(path); // + @"\"));
string uncpath = @"\\" + pc + @"\" + ffp.Replace(":", "$");
return uncpath;
}
如果这不起作用,则说明您存在权限问题。也许来自VM,您无权在远程文件夹上写内容。