收到异常“参数不正确。\r\n”移动文件时
我已经编写了一段代码来移动文件,如下所示
private void Move_Click(object sender, EventArgs e)
{
string strOrgpath = string.Empty, strNewpath = string.Empty;
strOrgpath = tvwACH.SelectedNode.ToString();
string strPath = strOrgpath.Substring(10);
FolderBrowserDialog folderborwser1 = new FolderBrowserDialog();
if (folderborwser1.ShowDialog() == DialogResult.OK)
{
try
{
strNewpath = folderborwser1.SelectedPath;
File.Move(strPath, strNewpath);
}
catch (Exception ex)
{
}
}
}
,但是正如我提到的,我遇到了异常,任何人都可以告诉我原因,有时我会收到错误,因为对路径的访问被拒绝
I have written a code to move a file as follows
private void Move_Click(object sender, EventArgs e)
{
string strOrgpath = string.Empty, strNewpath = string.Empty;
strOrgpath = tvwACH.SelectedNode.ToString();
string strPath = strOrgpath.Substring(10);
FolderBrowserDialog folderborwser1 = new FolderBrowserDialog();
if (folderborwser1.ShowDialog() == DialogResult.OK)
{
try
{
strNewpath = folderborwser1.SelectedPath;
File.Move(strPath, strNewpath);
}
catch (Exception ex)
{
}
}
}
But i am getting the exception as i mentioned can any one tell why and some times i am getting the error as access to the path is denied
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
确保您的子字符串调用返回正确的结果。如果可能,请改用 Path 类中的静态方法。查看 MSDN 页面的 File.Move< /a> 并注意需要哪些参数 - 您应该提供两个有效的完整文件名(例如
C:\Blah\myFile.txt
)。如果用户在文件夹浏览器对话框中选择他们没有写入权限的文件夹,则可能会出现“访问被拒绝”错误消息。您必须在代码中处理这种情况,也许可以通过捕获
UnauthorizedAccessException
来处理。更新:目标文件还应该指向文件名。所以你需要做这样的事情:
Make sure your substring call returns the correct result. If possible, use static methods from the
Path
class instead. Take a look at the MSDN page for File.Move and pay attention to what parameters are expected -- you should provide two valid full file names (e.g.C:\Blah\myFile.txt
)."Access denied" error message might happen if the user picks a folder they don't have write access to in the folder browser dialog. That's a scenario you'll have to handle in your code, perhaps by catching the
UnauthorizedAccessException
.Update: the destination file should also point to a filename. So you'll need to do something like this:
如果没有看到运行时应用程序中使用的值,我猜测
tvwACH.SelecteNode.ToString()
或strOrgpath.Substring(10)
不是有效的文件系统路径。您应该调试您的应用程序并设置一个断点以查看这些值是什么(如果此时您的问题不明显,则发布它们)。
Without seeing the values that are being used in your application at run-time, I'm guessing
tvwACH.SelecteNode.ToString()
orstrOrgpath.Substring(10)
is not a valid File System path.You should Debug your application and set a breakpoint to see what those values are (and post them if it's not obvious what your problem is at that point).
出现同样的错误,因为我试图在文件名中使用此符号之一
目标路径 '\ / : * ? “<>|'。
Had the same error because i was trying to use one of this symbols in file name
of destination path '\ / : * ? " < > |'.