【问题标题】:Using Visual Studio and Windows Forms (C#) to upload and rename file to network drive使用 Visual Studio 和 Windows 窗体 (C#) 将文件上传和重命名到网络驱动器
【发布时间】:2018-10-25 14:27:50
【问题描述】:

这是我正在处理的一些代码:

    private void Form1_Load(object sender, EventArgs e)
    {

    }

    private void openFileDialog1_FileOk(object sender, CancelEventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        {
            OpenFileDialog openFileDialog1 = new OpenFileDialog
            {
                InitialDirectory = @"C:\",
                Title = "Add a PDF",

                CheckFileExists = true,
                CheckPathExists = true,

                DefaultExt = "pdf",
                Filter = "pdf files (*.pdf)|*.pdf",
                FilterIndex = 2,
                RestoreDirectory = true,

                ReadOnlyChecked = true,
                ShowReadOnly = true
            };

            if (openFileDialog1.ShowDialog() == DialogResult.OK)
            {
                textBox1.Text = openFileDialog1.FileName;
                string myFile = textBox1.Text;
                Console.WriteLine(myFile);

            }
        }
    }

    private void button2_Click(object sender, EventArgs e)
    {
        System.IO.File.Move(myFile, @"C:\testing\records\file.pdf");
    }
}

}

所以无论如何,在按钮 2 的底部,我正在尝试设置一些东西。我想添加一个使用 System.IO 行保存文件的按钮。但是当我添加按钮时,我无法让它正常工作。 “myFile”变量似乎不再被声明。我敢肯定这可能是今天任何人都会在这里粘贴的最混乱的代码,但是其中很多是由 Visual Studio 自动生成的,我不敢清理它,因为我不能 100% 确定其中一些是什么东西是。我尝试将按钮内容剪切并粘贴到更靠近 myFile 变量声明的位置,因为它是私有的,也许这就是它不再知道它的含义的原因。但是当我将它移到那里时,我在按钮调用开始时收到关于“私人”的不同错误。

【问题讨论】:

  • 您的问题只是“有可能吗?”...?编程中有很多事情是可能的。您实际上应该尝试它。本网站并非真正用于假设性问题。
  • 我正在尝试。我遇到了很多障碍,我似乎无法自己弄清楚如何去做。我用 Visual Studio 制作了几个表单,但我在这个上没有任何运气。
  • 那么就您遇到的具体问题提出问题是完全可以接受的。这个问题很宽泛,除了“是”之外并不能真正回答。您可能想阅读 this page 页面上的主题,了解什么是好问题。
  • 好的,我已经修改了我的问题,使其更加具体。我希望这更容易接受。
  • 老实说,最好的办法是删除并作为新问题发布。这已经被否决了 5 次,一个小时后你基本上改变了整个问题。你不会拿回这些选票。

标签: c# winforms


【解决方案1】:

这里的问题是您在 if 语句的范围内声明 myFile 变量,因此当该 if 语句块退出时 myFile 变量超出范围并且不再存在。

要使其工作,您需要将 myFile 变量的创建移至类级别。

public class Form1 : Form
{
    private string myFile;

    public Form1()
    {
        InitializeComponent();
    }

    // Other code snipped out
}

现在修改您的 button1_Click 代码,以便不再创建 myFile 的实例:

string myFile = textBox1.Text;

您只需填充您在代码主要部分中声明的私有变量:

myFile = textBox1.Text;

所以if 语句看起来像这样:

if (openFileDialog1.ShowDialog() == DialogResult.OK)
{
    textBox1.Text = openFileDialog1.FileName;
    myFile = textBox1.Text;
    Console.WriteLine(myFile);
}

你需要对 button2_Click 做一点修改:

private void button2_Click(object sender, EventArgs e)
{
    // Check that myFile has some text and isn't null.
    if (string.IsNullOrWhitespace(myFile))
        return;

    // Check that the file exists before attempting to move it.
    if (File.Exists(myFile))
        System.IO.File.Move(myFile, @"C:\testing\records\file.pdf");
}

【讨论】:

  • 非常感谢!我仍然看到的唯一问题是这一行: if (File.Exists(myFile)) System.IO.File.Move(myFile, @"C:\testing\records\file.pdf");那里的第一个“文件”抛出错误。 MVS 说“当前上下文中不存在名称“文件””。
  • File.ExistsSystem.IO 命名空间中,因此您可以通过将System.IO.File.Exists(myFile);using System.IO; 与其他使用行一起放在代码文件的顶部来进行资格预审.
  • 对于像这样主要用于文件操作的程序,我可能只想将 System.IO 放在顶部。嗯?考虑到我要移动和检查多少文件?
  • 是的,它让您不必一直输入整个命名空间。
猜你喜欢
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 2012-06-17
  • 2011-04-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多