2014-10-28 4 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

이 무슨 뜻 이죠, 당신을하지 않습니다

당신은 선택된 파일의 전체 경로를 사용해야합니까? 런타임 예외가 발생합니까? –

+0

@GrantWinney 내 말은 .. 위의 코드는 내가 일을 할 수있게 해준다.하지만 System.IO.File.Copy (path, newpath);'파일을 디버그 폴더에 복사하고 싶지 않다. .. 파일을 선택하고 직접 디버그 폴더로 복사하는 대신 컴퓨터로 보내려고합니다. –

+0

img_path 및 path의 값은 무엇입니까? – Adam47

답변

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

아니 지금은 작동하지 !! 프린터는 지금 인쇄하지 않습니다 .. –

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를 열면 파일의 현재 디렉터리 (이 경우 bin/debug)에 대한 EXE 파일의 위치를 ​​기준으로 찾습니다. "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;'를주었습니다 .. 파일 이름과 확장자가 필요합니다. –

+1

그렇지만 전체 파일 경로가 필요합니다. 예 : "c : \ temp \ somefile.jpg" "somefile.jpg"와 같은 값으로 작업하고 있습니다. – Mark