C#Windows窗体应用程序:从打开对话框和SaveAs到应用程序目录中选择文件。 [英] C# Windows Form Application: Select Files from Open Dialog and SaveAs to Application Directory.

查看:380
本文介绍了C#Windows窗体应用程序:从打开对话框和SaveAs到应用程序目录中选择文件。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我有一个基于Windows窗体的应用程序。我的要求是从安装此应用程序的机器中选择一些文本文件,并将所有文件保存到我的应用程序目录中以供将来参考。就像在ASP.NET中将照片/符号上传到Web服务器一样。



这是基于Windows Form的应用程序吗?

Hi,
I have an Windows Form based application. My requirement is to select some Text files from the machine where this application is installed and SaveAs all the files into my Application directory for future references. It is just like in ASP.NET to upload your photo/sign to web server.

Is it a way in Windows Form Based application?

推荐答案

并非所有查找可执行文件目录的方法都适用于所有情况。这种方式适用于所有情况:

如何找到我的程序目录 [ ^ ]。



您可以使用属性 FileDialog.InitialDirectory

https: //msdn.microsoft.com/en-us/library/7x92870z%28v=vs.110%29.aspx [ ^ ]。



System.Windows.Forms 应用程序,您只需使用 System.Windows.Forms.Application.StartupPath



但我不建议将任何文件保存到此目录中。最典型的情况是,用户无法获得该权限。将目录选择留给用户要好得多。另请注意,文件对话框将持久使用的最后一个目录存储在用户的数据中。



-SA
Not all ways of finding executable directory work in all cases. This way works correctly in all cases:
How to find my programs directory[^].

You can use the path you want using the property FileDialog.InitialDirectory:
https://msdn.microsoft.com/en-us/library/7x92870z%28v=vs.110%29.aspx[^].

With a System.Windows.Forms application, you can simply use System.Windows.Forms.Application.StartupPath.

But I would not recommend saving any files to this directories. Most typically, the user won't get permissions for that. It's much better to leave the choice of directory to the user. Also note that the file dialogs store the last directory used persistently, in the user's data.

—SA

OpenFileDialog openFileDialog1 = new OpenFileDialog();

openFileDialog1.InitialDirectory = @C:;

openFileDialog1.Title =浏览文本文件 ;

openFileDialog1.CheckFileExists = true;

openFileDialog1.CheckPathExists = true;

openFileDialog1.DefaultExt =txt;

openFileDialog1.Filter =文本文件( .txt)| .txt |所有文件()| ;

openFileDialog1.FilterIndex = 2;

openFileDialog1.RestoreDirectory = true;

openFileDialog1.ReadOnlyChecked = true;

openFileDialog1。 ShowReadOnly = true;

if(openFileDialog1.ShowDialog()== DialogResult.OK)

{

filename = openFileDialog1.FileName;

}

File.Copy(filename,newpath + newfilename,true);

OpenFileDialog openFileDialog1 = new OpenFileDialog();
openFileDialog1.InitialDirectory = @"C:";
openFileDialog1.Title = "Browse Text Files";
openFileDialog1.CheckFileExists = true;
openFileDialog1.CheckPathExists = true;
openFileDialog1.DefaultExt = "txt";
openFileDialog1.Filter = "Text files (.txt)|.txt|All files (.)|.";
openFileDialog1.FilterIndex = 2;
openFileDialog1.RestoreDirectory = true;
openFileDialog1.ReadOnlyChecked = true;
openFileDialog1.ShowReadOnly = true;
if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
filename = openFileDialog1.FileName;
}
File.Copy(filename, "newpath+newfilename" , true);


这篇关于C#Windows窗体应用程序:从打开对话框和SaveAs到应用程序目录中选择文件。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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