从不同位置保存文件的问题 [英] problem with saving files from different locations

查看:59
本文介绍了从不同位置保存文件的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个代码用于将文件保存到sql server



public byte [] ReadDoc(string document)

{

byte [] DocData = null;

DirectoryInfo dirInfo = new DirectoryInfo(document);

foreach(dirInfo.GetFiles()中的FileInfo f) />
{

long DocFileLength = f.Length;

FileStream fs1 = new FileStream(document,FileMode.Open,FileAccess.Read);

BinaryReader br1 = new BinaryReader(fs1);

DocData = br1.ReadBytes((int)DocFileLength);

}



返回DocData;



}

和保存按钮事件我有



if(listOfLoadedFiles.Items.Count!= 0)

{

byte [] fileData = null;



foreach(ListOfLoadedFiles.Items中的ListViewItem项目)

{



字符串扩展名= Path.GetExtension(item.Text);



fileData = ReadDoc(item.Text);



daData2.InsertCommand = new SqlCommand(INSERT INTO Documents VALUES(@ Document,@ patternID,@ userID,@ docmentName,@ extension),cs);



daData2.InsertCommand.Parameters.Add(@ Documents,SqlDbType.VarBinary).Value = fileData;

daData2.InsertCommand.Parameters.Add(@ patternID,SqlDbType.NVarChar).Value = patternID.Text;

daData2.InsertCommand.Parameters.Add(@ userID,SqlDbType.NVarChar).Value = ID。文字;

daData2.InsertCommand.Parameters.Add(@ documentName,SqlDbType.NVarChar).Value = item.Text;

daData2.InsertCommand.Parameters.Add( @extension,SqlDbType.NVarChar).Value = extension;



cs.Open();

daData2.InsertCommand.ExecuteNonQuery( );

cs.Close();



}



}

其他

{



}



,问题是当我从不同文件夹保存文件时,似乎FileInfo中保存的路径只记得最后一个



添加到listview的文件(listOfLo adedFiles),当我点击Save按钮时,我收到错误就行了



长DocFileLength = f.Length;无法找到该文件



任何帮助,谢谢

I have this code for saving files into sql server

public byte[] ReadDoc(string document)
{
byte[] DocData = null;
DirectoryInfo dirInfo = new DirectoryInfo(document);
foreach (FileInfo f in dirInfo.GetFiles())
{
long DocFileLength = f.Length;
FileStream fs1 = new FileStream(document, FileMode.Open, FileAccess.Read);
BinaryReader br1 = new BinaryReader(fs1);
DocData = br1.ReadBytes((int)DocFileLength);
}

return DocData;

}
and on save button event I have

if (listOfLoadedFiles.Items.Count != 0)
{
byte[] fileData = null;

foreach (ListViewItem item in listOfLoadedFiles.Items)
{

string extension = Path.GetExtension(item.Text);

fileData = ReadDoc(item.Text);

daData2.InsertCommand = new SqlCommand("INSERT INTO Documents VALUES (@Documents, @patternID, @userID, @documentName, @extension)", cs);

daData2.InsertCommand.Parameters.Add("@Documents", SqlDbType.VarBinary).Value = fileData;
daData2.InsertCommand.Parameters.Add("@patternID", SqlDbType.NVarChar).Value = patternID.Text;
daData2.InsertCommand.Parameters.Add("@userID", SqlDbType.NVarChar).Value = ID.Text;
daData2.InsertCommand.Parameters.Add("@documentName", SqlDbType.NVarChar).Value = item.Text;
daData2.InsertCommand.Parameters.Add("@extension", SqlDbType.NVarChar).Value = extension;

cs.Open();
daData2.InsertCommand.ExecuteNonQuery();
cs.Close();

}

}
else
{

}

and the problem is when I am saving files from different folders, it seems that saved path in FileInfo remebers only the last

added file to listview (listOfLoadedFiles), and when I click Save button, i get error on line

long DocFileLength = f.Length; that file couldn't be found

any help, thank you

推荐答案

在我们看之前,并尝试解决它,你想做什么?您是否意识到您的ReadDoc方法将返回它在您指定的文件夹中找到的最后一个文件的数据?因此,如果有六个文件,它将全部读取,并返回最后一个 - 无论哪个恰好在您的应用程序运行的系统中?并且很有可能它会因为大量文件夹上的拒绝访问原因而失败?

如果你传递了一个代码所暗示的文件名,那么它会失败吗?如果它是路径,那么您的扩展信息将是错误的?

如果您指定要读取的文件而不是路径,它可以被File.ReadAllBytes替换?





请参阅下面的OP评论,代码为



Ok ,ListView中是否有单独的文件名?

因此每个ListViewItem都是一个文件,包含它的路径。

阅读文件内容:

Before we look at that, and try to sort it out, what are you trying to do? You do realize that your ReadDoc method will return the data for the last file it finds in the folder you specify? So if there are six files, it will read them all, and return the final one - whichever that happens to be in the system your app is running in? And that there is a very good chance that it will fail for access denied reasons on a huge number of folders?
And that if you pass a filename as your code implies, it will fail anyway? And if it's a path, then your extension info will be wrong?
And that it could be replaced by File.ReadAllBytes if you specify the file you want to read rather than a path?


See OP comments below with code

Ok, so the ListView has individual file names in it?
So each ListViewItem is a single file, complete with it's path.
To read the file content:
foreach (ListViewItem item in listOfLoadedFiles.Items)
    {
    string path = item.Text;
    if (!string.IsNullOrWhiteSpace(path) && File.Exists(path))
        {
        byte[] data = File.ReadAllBytes(path);
        long length = new FileInfo(path).Length;
        string extension = Path.GetExtension(path);
        ... continue and write the file to SQL.
        }
    }



完全转储ReadDoc方法!


And dump the ReadDoc method completely!


在你的代码中(我认为)获取目录路径,并读取其中的所有文件:每次'foreach循环执行其内部代码时,您正在创建和使用所有变量。所以你扔掉字节数组'DocData的内容,除了最后一次循环执行。我认为这可能是你问题的根源。
In your code that (I think) gets a directory path, and reads all the files in it: you are creating, and using, all the variables each time the 'foreach loop executes its inner code. So you "throw away" the contents of the byte array 'DocData, except for the last time the loop executes. I think that's probably the source of your problem.


这篇关于从不同位置保存文件的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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