如何在path中使用字符串变量
本文关键字:字符串 变量 path | 更新日期: 2023-09-27 18:12:28
如何在系统路径中使用字符串变量?下面是c#代码的示例:
public class Test
{
public Item Met()
{
string file_name = "sample1.pdf";
///I' m just giving the code where I have the problem, not full code
/// kindly ignore the syntax errors if any
FileStream fileStream = File.OpenRead("c:''Temp''sample1.pdf");
// Here I tried "C:''Temp''" + file_name //
string requestBodyStart = "'r'n'r'n--BOUNDARY'r'n" +
"Content-Type: application/xml'r'n" +
"Content-Disposition: form-data'r'n" +
"'r'n" +
envDef + "'r'n'r'n--BOUNDARY'r'n" +
"Content-Type: application/pdf'r'n" +
"Content-Disposition: file;filename='"sample2.pdf'"; documentId=1'r'n" +
"'r'n"; ///Here in place of "sample.pdf" I want to use variable name
string requestBodyEnd = "'r'n--BOUNDARY--'r'n'r'n";
在第二个案例中,我尝试了"Content-Disposition: file;file_name='" + file_name +'"
但是我得到了这个:
无法识别的转义序列,意外字符'''
在路径中使用变量是正确的吗?
谢谢。
使用路径。组合方法连接两个字符串路径。
string file_name = "sample1.pdf";
FileStream fileStream = File.OpenRead(Path.Combine("c:''Temp", file_name);
也考虑using
语句,因为FileStream
实现了IDisposable
。
查看Path.Combine Method (String, String)
方法
将两个字符串组合成一个路径。
您的语句中忘记了两个"
"Content-Disposition: file;filename='"" + file_name + "'"; documentId=1'r'n" +
但我更喜欢String.Format
String.Format("Content-Disposition: file;filename='"{0}'"; documentId=1'r'n", file_name) +
你最好创建一个读取文件的下载链接。
。aspx:
<asp:Panel ID="AttachmentPanel" runat="server" Visible="false">
<asp:HyperLink ID="DownloadHyperLink" runat="server" Text="Download Attachment"</asp:HyperLink>
</asp:Panel>
cs:
string file_name = "http://192.168.100.1:400/sample1.pdf";
this.DownloadHyperLink.Attributes.Add("OnClick", "window.open('" + file_name + "')");
将字符串路径中的'''替换为''''