【问题标题】:Always asking file not present in bin/debug folder总是询问 bin/debug 文件夹中不存在的文件
【发布时间】:2014-12-24 08:42:01
【问题描述】:

有什么方法可以从原始位置获取数据,而不是将文件复制到 bin/debug 文件夹。 我正在寻找将文件发送到我的本地打印机。但是我的 C# 应用程序只允许在我将文件复制到我的 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);
    }
}

【问题讨论】:

  • 什么意思,它不让你?您是否遇到运行时异常?
  • @GrantWinney 我的意思是.. 上面写的代码允许我完成这项工作.. 但我不想将System.IO.File.Copy(path, newpath); 文件复制到调试文件夹.. 我想选择文件并直接发送到机器,而不是复制到调试文件夹。
  • img_path 和 path 的取值是多少?
  • 你为什么要这样做if (!File.Exists(filename_noext))?不应该是if (!File.Exists(ofd.FileName))吗?

标签: c# wpf visual-studio-2010 visual-studio file-copying


【解决方案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;
}

【讨论】:

    【解决方案2】:

    看起来根本问题是由以下几行引起的:

    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;
    }
    

    【讨论】:

    • 不是那个..我给了img_path.Text = filename_noext;一些其他原因..我需要文件名和扩展名
    • 对,但您还需要完整的文件路径。示例:“c:\temp\somefile.jpg”您正在使用如下值:“somefile.jpg”
    猜你喜欢
    • 2010-09-13
    • 2011-04-21
    • 1970-01-01
    • 2018-04-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-21
    相关资源
    最近更新 更多