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

相关问题