0

システム パスで文字列変数を使用するにはどうすればよいですか? 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";

私が試した2番目のケースでは"Content-Disposition: file;file_name=\" + file_name +\" 、しかし、私はこれを得ています:

認識できないエスケープ シーケンス、予期しない文字 '\'

これはパスで変数を使用する正しい方法ですか?

ありがとうございました。

4

5 に答える 5

6

Path.Combineメソッドを使用して、2 つの文字列パスを連結します。

string file_name = "sample1.pdf";
FileStream fileStream = File.OpenRead(Path.Combine("c:\\Temp", file_name);

実装using以降のステートメントも検討してください。FileStreamIDisposable

于 2013-05-07T06:16:51.500 に答える
0

ファイルを読み取るためのダウンロードリンクを作成することをお勧めします。

.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 + "')");
于 2013-05-07T06:39:35.667 に答える
-2

文字列パスの「\」を「\\」に置き換えます

于 2013-05-07T06:16:55.847 に答える