C#中的异常问题 [英] Exception Problem in C#

查看:131
本文介绍了C#中的异常问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好,即时通讯使用VS2005,我有一个网页,其中有一个FileUpload控件和一个Submit按钮。在提交按钮(.cs)页面上,我调用此行:

FileStream FilStr = new FileStream(FileUpload1.FileName,FileMode.Open);
BinaryReader BinRed = new BinaryReader(FilStr);

但是在运行时每当我选择一个文件然后单击提交按钮时,它会在上面的代码行中给出以下异常:

System.IO.FileNotFoundException:找不到文件' C:\WINDOWS\system32\test_logo.PNG"。文件名:System.IO上的'C:\ WINDOWS \system32 \ test_logo.PNG'。在System.IO.FileStream.Init中的Error.WinIOError(Int32 errorCode,String maybeFullPath)(字符串路径,FileMode模式,FileAccess访问) ReportsDynamicImage.btnSubmit_Click(Object sender,EventArgs)上的System.IO.FileStream..ctor(String path,FileMode模式),Int32权限,布尔值useRights,FileShare共享,Int32 bufferSize,FileOptions选项,SECURITY_ATTRIBUTES secAttrs,String msgPath,Boolean bFromProxy) e)在d:\Projects

Hello everyone, im using VS2005, i have a webpage in which i have a FileUpload control, and a Submit button. On the Submit button (.cs)page, i call this line :

FileStream FilStr = new FileStream(FileUpload1.FileName, FileMode.Open);
BinaryReader BinRed = new BinaryReader(FilStr);

but at run time whenever i select a file and then click on submit button, it gives me the following exception at the above line of code:

System.IO.FileNotFoundException: Could not find file 'C:\WINDOWS\system32\test_logo.PNG'. File name: 'C:\WINDOWS\system32\test_logo.PNG' at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath) at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy) at System.IO.FileStream..ctor(String path, FileMode mode) at Reports_DynamicImage.btnSubmit_Click(Object sender, EventArgs e) in d:\Projects

plz告诉我这有什么问题,并且它看起来在C:\中的图像\窗口,因为图像是我的文档的Mypictures foders。我还包括使用System.IO命名空间,但仍然存在此异常。帮助我紧急回复我asap。

plz tell me whats wrong with this, and y its looking the image in the C:\\windows as the image is in the Mypictures foders of the My Documents. I includede the using System.IO namespace also but this exception still comes. help me its urgent
reply me asap.


Thanx in Advance。


Thanx in Advance.

推荐答案

FileName只提供文件的名称。不完整路径

The FileName just gives the name of the file. Not a complete path

如果要打开文件流,可以使用FileContent属性。 SubmitButton事件处理程序的代码如下所示:

If you want to open a stream to the file you could use the FileContent property. The code for the SubmitButton eventhandler looks like this: 

Stream stream = FileUpload1.FileContent;

Stream stream = FileUpload1.FileContent;

BinaryReader reader = new BinaryReader (stream);

如果您想上传文件,可以使用SaveAs方法:

string completePath = 路径 .Combine( " c:\\MyUploads" ,FileUpload1.FileName);

string completePath = Path.Combine("c:\\MyUploads",FileUpload1.FileName);

FileUpload1.SaveAs(completePath);

FileUpload1.SaveAs(completePath);


这篇关于C#中的异常问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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