我想打开一个特定的swj.mdb文件。
这个文件可放在任何位置。
如果用户选择的不是这个文件,那么系统就会提示“请选择你机器上的swj.mdb文件”private void swjopenFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
if(!swjopenFileDialog.CheckFileExists)
{
}
}
具体代码应该怎样写呢?
这个文件可放在任何位置。
如果用户选择的不是这个文件,那么系统就会提示“请选择你机器上的swj.mdb文件”private void swjopenFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
if(!swjopenFileDialog.CheckFileExists)
{
}
}
具体代码应该怎样写呢?
string url=myfile.Value.ToString().Trim();
if(url==null||url=="")
{
Response.Write("<script>alert('请选择文件名!')</script>");
return;
}
System.IO.FileInfo fi=new FileInfo(url);
string filename=fi.Name.ToString();//取文件名
string extname=fi.Extension.ToString();//取文件后缀
if(filename!="swj.mdb")
{
Response.Write("<script>alert('请选择您机器上的swj.mdb文件!')</script>");
return;
} if(extname!=".mdb"||extname.Length<1)
{
Response.Write("<script>alert('数据导入失败,可能是文件格式错误!')</script>");
return;
}
myUrl=Server.MapPath("")+"\\upload"+"\\"+filename;
myfile.PostedFile.SaveAs(myUrl);
我怎么判断这个路径里是否包含swj.mdb
能给我简单的代码吗
一运行就出现错误。正确的应该怎样写呢?
其次才是在服务器端验证。
{
OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.ShowReadOnly = false;
ofDlg.Filter = "数据库文件(swj.mdb)|swj.mdb";;
ofDlg.Title = "请选择 swj.mdb 文件";
ofDlg.CheckFileExists = true;
ofDlg.CheckPathExists = true;
ofDlg.ValidateNames = true;
ofDlg.Multiselect = false;
DialogResult y = ofDlg.ShowDialog();
if (y == DialogResult.OK)
{
string fileName = ofDlg.FileName;
if(fileName.Contains('\\'))
fileName = fileName.SubString(fileName.LastIndexOf('\\'));
if(fileName != "swj.mdb")
{
MessageBox.Show(this, "选择的文件不正确!"); // 提示错误信息。
e.Cancel = true; // 取消事件??
}
}
else e.Cancel = true; //取消事件??
}
不过不严格,比如 hacker.swj.mdb 用这种方法也能通过。
可以进一步判断: swj.mdb 后面没有字符,swj.mdb 前一个字符是 \ 才合法
fileName = fileName.SubString(fileName.LastIndexOf('\\'));
“fileName”并不包含对“Contains”的定义
OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.ShowReadOnly = false;
ofDlg.Filter = "数据库文件(swj.mdb)|swj.mdb";;
ofDlg.Title = "请选择 swj.mdb 文件";
ofDlg.CheckFileExists = true;
ofDlg.CheckPathExists = true;
ofDlg.ValidateNames = true;
ofDlg.Multiselect = false;
DialogResult y = ofDlg.ShowDialog();
这段代码写在buttom_click事件里没问题吧?将
DialogResult y = ofDlg.ShowDialog();
if (y == DialogResult.OK)
{
string fileName = ofDlg.FileName;
if(fileName.Contains('\\'))
fileName = fileName.SubString(fileName.LastIndexOf('\\'));
if(fileName != "swj.mdb")
{
MessageBox.Show(this, "选择的文件不正确!"); // 提示错误信息。
e.Cancel = true; // 取消事件??
}
}
else e.Cancel = true; //取消事件??
}写在swjopenFileDialog_FileOk事件里。
{
if(fileName.IndexOf('\\') >= 0) // 判断文件名中是否包含路径分隔符
fileName = fileName.SubString(fileName.LastIndexOf('\\') + 1); //存在的话获取最后一个路径分隔符后的内容作为文件名
return (fileName == "swj.mdb"); //判断文件名是否为给定的文件名
}private void btnSelectFile_Click(object sender, System.EventArgs e)
{
OpenFileDialog ofDlg = new OpenFileDialog(); // 初始化打开文件对话框的新实例
ofDlg.ShowReadOnly = false; // 隐藏“以只读方式打开”复选框
ofDlg.Filter = "数据库文件(swj.mdb)|swj.mdb";// 设置文件过滤器,
// 只让对话框中显示 swj.mdb 文件
ofDlg.Title = "请选择 swj.mdb 文件"; // 设置对话框标题
ofDlg.CheckFileExists = true; // 要求打开的文件必须存在
ofDlg.CheckPathExists = true; // 要求打开的路径必须存在
ofDlg.ValidateNames = true; // 验证输入的文件名是否符合文件名命名规范
ofDlg.Multiselect = false; // 禁止选择多个文件
DialogResult y = ofDlg.ShowDialog(); // 显示对话框,并保存返回结果
if (y == DialogResult.OK) // 如果用户按的是“确定”按钮
if(ValiadateFileName(ofDlg.FileName)) // 验证文件名
{
// 验证通过,进行进一步操作
}
else
{
// 验证失败,提示,取消操作
MessageBox.Show(null, "请选择 swj.mdb 文件!");
}
}
private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog swjopenFileDialog=new OpenFileDialog();
swjopenFileDialog.InitialDirectory="D:\\qqq/qq";
swjopenFileDialog.Filter="数据库文件|*.mdb";
swjopenFileDialog.FileName="swj.mdb";
swjopenFileDialog.RestoreDirectory=true;
swjopenFileDialog.CheckFileExists = true;
swjopenFileDialog.CheckPathExists = true;
swjopenFileDialog.ValidateNames = true;
swjopenFileDialog.Multiselect = false; swjopenFileDialog.FilterIndex=1;
DialogResult y = swjopenFileDialog.ShowDialog(); // 显示对话框,并保存返回结果
if (y == DialogResult.OK) // 如果用户按的是“确定”按钮
if(ValiadateFileName(swjopenFileDialog.FileName)) // 验证文件名
{
// 验证通过,进行进一步操作
}
else
{
// 验证失败,提示,取消操作
MessageBox.Show("请选择swj.mdb 文件!");
}
else
{
this.Close();
}
一执行就会出现异常!
swjopenFileDialog.FilterIndex=1;
为什么设为 1 呢?文件类型筛选器是从 0 起的吧?而且,为什么你要 this.Close() 呢?swjopenFileDialog.CheckFileExists = true;有这段代码以后,不可能一个文件不选允许你点确定来关闭打开文件对话框的,难道异常是对话框执行时抛出的?
“打开文件”对话框的,而且也没有产生任何异常。
如果你希望别人只选择 "swj.mdb", 应该在筛选器设置时直接筛选出 swj.mdb 如下面:swjopenFileDialog.Filter = "数据库文件(swj.mdb)|swj.mdb";这样对话框中只会显示名字为 swj.mdb 的文件其它文件一概不显示……)
你用的是 Vs 2005 还是 2003 ?
高亮显示的代码不一定都是异常啊,断点也会高亮显示的。产生异常时,如果你用 Vs 2005 的话,应该会有一个“异常助手”显示并指向高亮代码;如果是 Vs 2003 的话,应该先弹出一个对话框,曰:“未处理的异常 xxxxxException 发生在 XXXXXX”“其它信息:XXXXX”,点击“继续”后才会转向高亮代码。如果以上两种情况都没有的话,那么就表明高亮显示很可能是你设定的断点了:那行代码的最左边是不是有一个红色的实心圆圈?那个就是断点的标志。再次点击那个红圈就可以取消断点。有时不小心可能会误点到那里误设断点的。
未处理的“System.ObjectDisposedException”类型的异常出现在 system.windows.forms.dll 中。其他信息: 无法访问名为“login”的已处置对象。此login是登陆窗体!
{
OpenFileDialog swjopenFileDialog=new OpenFileDialog();
swjopenFileDialog.InitialDirectory="D:\\信息接收/db";
swjopenFileDialog.Filter="数据库文件|swj.mdb";
swjopenFileDialog.FileName="swj.mdb";
swjopenFileDialog.RestoreDirectory=true;
swjopenFileDialog.CheckFileExists = true;
swjopenFileDialog.CheckPathExists = true;
swjopenFileDialog.ValidateNames = true;
swjopenFileDialog.Multiselect = false; //swjopenFileDialog.FilterIndex=1;
DialogResult y = swjopenFileDialog.ShowDialog(); // 显示对话框,并保存返回结果
if (y == DialogResult.OK) // 如果用户按的是“确定”按钮
if(ValiadateFileName(swjopenFileDialog.FileName)) // 验证文件名
{
MessageBox.Show("通过");// 验证通过,进行进一步操作
}
else
{
// 验证失败,提示,取消操作
MessageBox.Show("请选择User.mdb 文件!");
}
else
{
this.Close();
}
}
代码应是怎样的呢?
string url=myfile.Value.ToString().Trim();
if(url==null||url=="")
{
Response.Write("<script>alert('请选择文件名!')</script>");
return;
}
System.IO.FileInfo fi=new FileInfo(url);
string filename=fi.Name.ToString();//取文件名
string extname=fi.Extension.ToString();//取文件后缀
if(filename!="swj.mdb")
{
Response.Write("<script>alert('请选择您机器上的swj.mdb文件!')</script>");
return;
}if(extname!=".mdb"||extname.Length<1)
{
Response.Write("<script>alert('数据导入失败,可能是文件格式错误!')</script>");
return;
}
myUrl=Server.MapPath("")+"\\upload"+"\\"+filename;
myfile.PostedFile.SaveAs(myUrl);.Net 2.0 中可以使用 System.IO.Path.GetFileName(string); 来获取物理路径中的文件名。如果还是 .Net 1.1 的话,可以 这样string swjfileName = swjopenFileDialog.FileName.SubString(swjopenFileDialog.FileNaem.LastIndexOf('\\') + 1);如果是 Url ,就把斜杠反向。private void swjopenFileDialog_FileOk(object sender, System.ComponentModel.CancelEventArgs e)
{
OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.ShowReadOnly = false;
ofDlg.Filter = "数据库文件(swj.mdb)|swj.mdb";;
ofDlg.Title = "请选择 swj.mdb 文件";
ofDlg.CheckFileExists = true;
ofDlg.CheckPathExists = true;
ofDlg.ValidateNames = true;
ofDlg.Multiselect = false;
DialogResult y = ofDlg.ShowDialog();
if (y == DialogResult.OK)
{
string fileName = ofDlg.FileName;
if(fileName.Contains('\\'))
fileName = fileName.SubString(fileName.LastIndexOf('\\'));
if(fileName != "swj.mdb")
{
MessageBox.Show(this, "选择的文件不正确!"); // 提示错误信息。
e.Cancel = true; // 取消事件??
}
}
else e.Cancel = true; //取消事件??
}OpenFileDialog ofDlg = new OpenFileDialog();
ofDlg.ShowReadOnly = false;
ofDlg.Filter = "数据库文件(swj.mdb)|swj.mdb";;
ofDlg.Title = "请选择 swj.mdb 文件";
ofDlg.CheckFileExists = true;
ofDlg.CheckPathExists = true;
ofDlg.ValidateNames = true;
ofDlg.Multiselect = false;
DialogResult y = ofDlg.ShowDialog();
这段代码写在buttom_click事件里没问题吧?将
DialogResult y = ofDlg.ShowDialog();
if (y == DialogResult.OK)
{
string fileName = ofDlg.FileName;
if(fileName.Contains('\\'))
fileName = fileName.SubString(fileName.LastIndexOf('\\'));
if(fileName != "swj.mdb")
{
MessageBox.Show(this, "选择的文件不正确!"); // 提示错误信息。
e.Cancel = true; // 取消事件??
}
}
else e.Cancel = true; //取消事件??
}private void button1_Click(object sender, System.EventArgs e)
{
OpenFileDialog swjopenFileDialog=new OpenFileDialog();
swjopenFileDialog.InitialDirectory="D:\\信息接收/db";
swjopenFileDialog.Filter="数据库文件|swj.mdb";
swjopenFileDialog.FileName="swj.mdb";
swjopenFileDialog.RestoreDirectory=true;
swjopenFileDialog.CheckFileExists = true;
swjopenFileDialog.CheckPathExists = true;
swjopenFileDialog.ValidateNames = true;
swjopenFileDialog.Multiselect = false; //swjopenFileDialog.FilterIndex=1;
DialogResult y = swjopenFileDialog.ShowDialog(); // 显示对话框,并保存返回结果
if (y == DialogResult.OK) // 如果用户按的是“确定”按钮
if(ValiadateFileName(swjopenFileDialog.FileName)) // 验证文件名
{
MessageBox.Show("通过");// 验证通过,进行进一步操作
}
else
{
// 验证失败,提示,取消操作
MessageBox.Show("请选择User.mdb 文件!");
}
else
{
this.Close();
}
不然可能极为复杂
你可以不设定初始文件名,
// swjopenFileDialog.FileName="swj.mdb"; //取消这行
然后设定
swjopenFileDialog.CheckFileExists = true;
这样系统不会允许用户不选择文件点确定。