如何将Pdf文件保存到C#中的当前工作目录中 [英] How Do I Save A Pdf File Into Current Working Directory In C#

查看:157
本文介绍了如何将Pdf文件保存到C#中的当前工作目录中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用C#开发Windows应用程序.在开发pdf文件时,我想保存到当前工作目录中.这意味着在客户端计算机中,pdf文件应保存到应用程序目录中.任何内容,它将保存的位置.但是它应保存在当前目录中,这表示客户端在系统中安装应用程序的位置.

Hi i''m developing windows application in c#.While developing the pdf file i want to save into current working directory.That means in the client machine the pdf file should be save into application directory.It shouldn''t ask anything,where it would save.But it should save in the current directory that means where client is installing the application in the system.

推荐答案

如果这是具有以下内容的应用程序: UI,在大多数情况下,最好的选择是使用另存为"对话框,以便用户可以选择目录.

但是,使用工作目录是非常典型的,尤其是对于仅控制台的应用程序.显然,该目录仍可以视为用户输入的一部分:这是用户启动应用程序的目录.如果以这种方式进行操作,则可以使用不带路径的文件名,因为工作目录具有优先权.请注意,应用程序目录和工作目录是不同的目录:用户可以在任何目录中启动应用程序.



还请注意与Richard MacCutchan的讨论,Richard MacCutchan给您有关应用程序目录的重要警告.这是可执行模块的放置目录(与安装无关);该应用程序无需任何安装即可运行.通常,此目录旨在仅提供对文件的只读访问.

工作目录有所不同:这是用户选择启动应用程序的位置(应用程序以后可以更改它),这是相对路径名的默认目录.如果用户选择具有写访问权限的目录(可以预料),则可以进行写操作. :-)

[END EDIT]

要查找与某个应用程序关联的目录,请参阅我过去的回答:
如何找到我的程序目录(如何找到我的程序目录().

切勿使用字符串连接和其他字符串操作来查找文件名和路径名.请注意类System.IO.Path的一些有用方法,尤其是:
https://msdn.microsoft.com/zh-cn/library/system.io.path.combine%28v = vs.110%29.aspx [ https://msdn.microsoft.com/en -us/library/system.io.path.getfilename(v = vs.110).aspx [ https://msdn.microsoft.com/en -us/library/system.io.path.getfilenamewithoutextension(v = vs.110).aspx [ https://msdn.microsoft.com/en -us/library/system.io.path.getfullpath(v = vs.110).aspx [
If this is an application with UI, the best option for most cases would be using "Save As" dialog, so the user would be able to choose the directory.

However, using a working directory is quite typical, especially for console-only applications. Apparently, this directory could still be considered as a part of the user input: this is the directory where the user started the application. If you do it this way, you can use the file name without path, because the working directory will have precedence. Note that the application directory and working directory are different directories: a user can start the application in any directory.



Also pay attention for our discussion with Richard MacCutchan, who gave you important warning about application directory. This is the directory where you executable module is placed (which is, in turn, has nothing to do with the installation; the application can work without any installations). Generally, this directory is intended to provide only read-only access to files.

Working directory is something different: this is where the user chooses to start the application (and the application can later change it), the default directory for relative path names. If the user choose the directory with write access (which is can be well expected), writing will be possible. :-)

[END EDIT]

For finding the directories associated with some application explicitly, please see my past answers:
How to find my programs directory (executable directory),
How to find my programs directory (current directory, "special folders").

Never use string concatenation and other string operation for finding file names and path names. Note some useful methods of the class System.IO.Path, in particular:
https://msdn.microsoft.com/en-us/library/system.io.path.combine%28v=vs.110%29.aspx[^] (do not concatenate, use these methods!),
https://msdn.microsoft.com/en-us/library/system.io.path.getfilename(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.path.getfilenamewithoutextension(v=vs.110).aspx[^],
https://msdn.microsoft.com/en-us/library/system.io.path.getfullpath(v=vs.110).aspx[^], and so on.

—SA


好吧,要完成此工作,您需要弄清楚当前的执行文件路径,为此,在.Net中我们有几种方法可以最有用的方法是:
第一部分:
Ok, for doing this job you need to figure out about current execution file path, in .Net for this purpose we have several ways some of the most useful ways are:
First part:
string exePath = Application.ExecutablePath;
string startupPath = Application.StartupPath;




Or

System.Reflection.Assembly.GetEntryAssembly().Location;




Or

string path = System.IO.Path.GetDirectoryName( 
      System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase );



但我更喜欢第一个. ;-)
然后,您需要确保在此目录下具有读取和写入权限,基本上,当您在其中一个系统用户下安装应用程序时,您将获得用户帐户服务所要求的特权,因此您无需关心它.您还可以在此作业的第二部分设置其他用户和权限.

第二部分:
这部分实际上取决于您的PDF创建模块,但是假设采用最流行的方法来进行此业务,则需要使用System.IO库.但是要设置权限,请尝试查看以下链接:
FileStream类别:
https://msdn.microsoft.com/zh-CN /library/system.io.filestream(v=vs.110).aspx [ https://msdn.microsoft.com/en -us/library/system.io.directory.setaccesscontrol(v = vs.110).aspx [ https://msdn.microsoft.com/en -us/library/system.io.fileinfo.setaccesscontrol(v = vs.110).aspx [



But I prefer first one. ;-)
Then you need to make sure you have a read and write permission at this directory, basically when you are installing application under one of the system users you will receive demanded privilege from user account service so you don`t need to care about it, however you can also set other user and permissions at the second part of this job.

Second Part:
This part is really depend on your PDF creation module, but by assuming the most poplar way to doing this business you will need to use System.IO library. But for setting permissions try to look at the below links:
FileStream Class:
https://msdn.microsoft.com/en-us/library/system.io.filestream(v=vs.110).aspx[^]
Directory.SetAccessControl Method:
https://msdn.microsoft.com/en-us/library/system.io.directory.setaccesscontrol(v=vs.110).aspx[^]
FileInfo.SetAccessControl Method:
https://msdn.microsoft.com/en-us/library/system.io.fileinfo.setaccesscontrol(v=vs.110).aspx[^]


获取答案以保存在当前应用程序启动路径.

Got an answer to save in current application startup path.

string filesave = String.Format(@"{0}", Application.StartupPath);


这篇关于如何将Pdf文件保存到C#中的当前工作目录中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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