参数异常错误,路径不是合法形式 [英] Argument exception error, the path is not of legal form

查看:114
本文介绍了参数异常错误,路径不是合法形式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

当我运行这段代码

 DirectoryInfo时,我得到一个参数异常(错误图像) directoryInfo =  new  DirectoryInfo(path); 



这是获取路径的代码

  private   void  openFolderToolStripButton_Click(< span class =code-keyword> object  sender,EventArgs e)
{
FolderBrowserDialog obd = new FolderBrowserDialog();
if (obd.ShowDialog()== DialogResult.OK)
{
path = obd.SelectedPath;
Console.WriteLine( 找到路径);
Console.WriteLine(path.ToString());

}
}





我的尝试:



它适用于预定义的路径,例如

 DirectoryInfo directoryInfo =  new  DirectoryInfo( @  C:\ Users \rohit \Desktop \all \ DS4Windows); 



我该怎么办,我有点困惑为什么这是一个问题

解决方案

< blockquote>由于页面加载上的路径是 empty / null ,它将导致参数异常,(基于截图的信息)



 FolderBrowserDialog obd = new FolderBrowserDialog(); 
if(obd.ShowDialog()== DialogResult.OK)
{
string path = obd.SelectedPath;
DirectoryInfo directoryInfo = new DirectoryInfo(path);
//从页面加载中剪切代码并将其粘贴到此处。

}






来自提供的截图好像你正试图在Form1_Load中这样做。你确定此时有一条有效的路径吗?如果它是您唯一的表单并且您没有从其他表单发送路径,那么这可能就是问题所在。在创建 DirectoryInfo 时,调试代码以查看路径变量中的实际内容。



希望这会有所帮助。


I get an argument exception(error image) when I run this piece of code

DirectoryInfo directoryInfo = new DirectoryInfo(path);


This is the code that gets path

private void openFolderToolStripButton_Click(object sender, EventArgs e)
{
    FolderBrowserDialog obd = new FolderBrowserDialog();
    if (obd.ShowDialog() == DialogResult.OK)
    {
        path = obd.SelectedPath;
        Console.WriteLine("path is found");
        Console.WriteLine(path.ToString());

    }
}



What I have tried:

It works fine with a predefined path e.g.

DirectoryInfo directoryInfo = new DirectoryInfo(@"C:\Users\rohit\Desktop\all\DS4Windows");


What should I do, I am a bit confused why this is a problem

解决方案

Since the path is empty/null on page load, it will result in argument exception, ( based on info from screenshot)

FolderBrowserDialog obd = new FolderBrowserDialog();
            if (obd.ShowDialog() == DialogResult.OK)
            {
               string path = obd.SelectedPath; 
                DirectoryInfo directoryInfo = new DirectoryInfo(path);
                // cut the code from page load and paste it over here.

            }


Hi,

from the provided screenshot it seems you're trying to do this in Form1_Load. Are you sure you have a valid path at this point? If it's your only form and you didn't send the path from some other form, this will probably be the issue. Debug your code to see what really is in the path variable in the moment you creating DirectoryInfo.

Hope this helps.


这篇关于参数异常错误,路径不是合法形式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆