将文件保存在特定的文件夹中. [英] Save a file in a specific folder.

查看:149
本文介绍了将文件保存在特定的文件夹中.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的项目(C#Windows应用程序)中,我要从USB或硬盘上传pdf,然后再次需要将相同的pdf文件存储在特定的文件夹中.如何保存?
我有一个显示上传文件路径的文本框.现在正在尝试保存文本框中的文件.但是保存的文件包含该文本作为在文本框中显示的路径.

In my project(C# windows application) i hav to upload a pdf from USB or harddisk and again i need to store the same pdf file in a specific folder.how to save it?
I hav a textbox which displays the uploaded file path. now am trying to save the file which is in the textbox. but the saved file contains the text as the path which diaplayed in the text box. how to save the file ??

推荐答案

这不是上载,而是指定文件的位置.您无需担心上传和保存.如果您只想将该文件保存到其他位置,请使用以下代码:

That is not uploading, but specifying the location of the file. You don''t need to bother about Uploading and Saving. If you just want to save that file to another location, use the following code:

string FileToCopy = null;
string NewCopy = null;

FileToCopy = "C:\\Users\\Owner\\Documents\\test.txt";
NewCopy = "C:\\Users\\Owner\\Documents\\NewTest.txt";


if (System.IO.File.Exists(FileToCopy) == true) {
    System.IO.File.Copy(FileToCopy, NewCopy);
    Interaction.MsgBox("File Copied");

}



您需要使用 System.IO;



you need to import system.io as


将system.io导入为

using System.IO;



Initialize the original path to FileToCopy variable and specify the Intended location to save the file in NewCopy variable.


在保存文件之前,您必须重命名文件名.

如果您上载的文件名类似于file.pdf,则在尝试保存时拆分文件名,直到找到字符``.'',然后在文件名旁边附加一些文本(例如_new),然后再次连接文件名和扩展名,然后将其保存为file_new .pdf.

如果您使用的是fileUpload,请尝试这样

You have to rename the file name before saving it.

If your uploaded filename is like file.pdf , when trying to save split the file name till you find character ''.'' and append some text next to file name (like _new) and concatenate filename and extension again and save it like file_new.pdf.

If you are using fileUpload then try like this

if (fileUpload1.HasFile)
{
string strPath = Server.MapPath("~");
strPath = strPath + "\\Resumes\\" + txtUsername.Text;
Directory.CreateDirectory(strPath);
strPath = strPath + "\\";
fileUpload1.SaveAs(strPath + fileUpload1.FileName);
}



如果要使用savefiledialog,请检查此链接
http://www.c-sharpcorner.com/uploadfile/mahesh/savefiledialog-in -C-Sharp/ [ ^ ]

如果要复制文件,
然后
http://msdn.microsoft.com/en-us/library/cc148994.aspx [ ^ ]
http://www.dotnetperls.com/file-copy [



If you want to use savefiledialog, then check this link
http://www.c-sharpcorner.com/uploadfile/mahesh/savefiledialog-in-C-Sharp/[^]

If you want to copy files,
then
http://msdn.microsoft.com/en-us/library/cc148994.aspx[^]
http://www.dotnetperls.com/file-copy[^]


这篇关于将文件保存在特定的文件夹中.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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