2014-10-28 68 views
-3

有什麼辦法可以從原始位置獲取數據,而不是將文件複製到bin/debug文件夾。 我正在尋找將文件發送到本地打印機。但是我的C#應用​​程序允許僅在將文件複製到我的bin/debug文件夾時發送文件。有沒有辦法我可以過來!總是詢問文件不存在bin/debug文件夾中

代碼:

private string image_print() 
{ 
    OpenFileDialog ofd = new OpenFileDialog(); 
     { 
      InitialDirectory = @"C:\ZTOOLS\FONTS", 
      Filter = "GRF files (*.grf)|*.grf", 
      FilterIndex = 2, 
      RestoreDirectory = true 
     }; 

    if (ofd.ShowDialog() == DialogResult.OK) 
    { 
     string filename_noext = Path.GetFileName(ofd.FileName); 
     string path = Path.GetFullPath(ofd.FileName); 
     img_path.Text = filename_noext; 

     string replacepath = @"bin\\Debug"; 
     string fileName = Path.GetFileName(path); 
     string newpath = Path.Combine(replacepath, fileName); 

     if (!File.Exists(filename_noext)) 
     { 
      // I don't like to copy the file to the debug folder 
      // is there an alternative solution? 
      File.Copy(path, newpath); 

      if (string.IsNullOrEmpty(img_path.Text)) 
      { 
       return ""; 
      } 

      StreamReader test2 = new StreamReader(img_path.Text); 
      string s = test2.ReadToEnd(); 
      return s; 
     } 
    } 
} 

private void button4_Click(object sender, EventArgs e) 
{ 
    string s = image_print() + Print_image(); 

    if (!String.IsNullOrEmpty(s) && 
     !String.IsNullOrEmpty(img_path.Text)) 
    { 
     PrintFactory.sendTextToLPT1(s); 
    } 
} 
+1

你是什麼意思,它不會讓你嗎?你得到一個運行時異常? – 2014-10-28 19:35:09

+0

@GrantWinney我的意思是..對於上面寫的代碼是讓我做的工作..但我不想複製'System.IO.File.Copy(路徑,新路徑);'該文件到調試文件夾..我想選擇文件並直接發送到機器,而不是複製到調試文件夾。 – 2014-10-28 19:53:55

+0

img_path和路徑的值是什麼? – Adam47 2014-10-28 19:56:27

回答

1

試試這個:

private string image_print() 
{ 
    string returnValue = string.Empty; 

    var ofd = new OpenFileDialog(); 
     { 
      InitialDirectory = @"C:\ZTOOLS\FONTS", 
      Filter = "GRF files (*.grf)|*.grf", 
      FilterIndex = 2, 
      RestoreDirectory = true 
     }; 

    if (ofd.ShowDialog() == DialogResult.OK && 
     !string.IsNullOrWhiteSpace(ofd.FileName) && 
     File.Exists(ofd.FileName)) 
    { 
     img_path.Text = Path.GetFileName(ofd.FileName); 

     using (var test2 = new StreamReader(ofd.FileName)) 
     { 
      returnValue = test2.ReadToEnd(); 
     } 
    } 

    return returnValue; 
} 
+0

沒有它現在不工作!打印機現在不打印.. – 2014-10-28 20:44:40

1

它看起來像潛在的問題是由這些線路引起的:

filename_noext = System.IO.Path.GetFileName(ofd.FileName); //this actually does include the extension!! It does not include the fully qualified path 
... 
img_path.Text = filename_noext; 
... 
StreamReader test2 = new StreamReader(img_path.Text); 

你從得到完整的文件路徑OpenFileDialog然後將img_path.Text設置爲文件名。然後您使用該文件名(無路徑)打開文件進行閱讀。

當您只用一個文件名打開一個新的StreamReader時,它會在當前目錄中查找該文件(相對於EXE的位置,在本例中爲bin/debug)。複製到「bin/debug」可能不適用於機器以外的任何其他環境,因爲EXE可能會部署在其他地方。

您應該使用完整路徑選擇的文件:

if (ofd.ShowDialog() == DialogResult.OK) 
{ 
    path = Path.GetFullPath(ofd.FileName); 
    img_path.Text = path; 
    if (string.IsNullOrEmpty(img_path.Text)) 
     return "";// 
    StreamReader test2 = new StreamReader(img_path.Text); 
    string s = test2.ReadToEnd(); 
    return s; 
} 
+0

它不是..我給了'img_path.Text = filename_noext;'由於其他一些原因..我需要文件名和擴展名 – 2014-10-28 20:45:20

+1

正確,但你也需要完整的文件路徑。 例如:「c:\ temp \ somefile.jpg」 您正在使用的值如下:「somefile.jpg」 – Mark 2014-10-28 20:52:53

相關問題