无法从 System.Windows.Forms.DialogResult 转换为 'String' C#
Cannot Convert From System.Windows.Forms.DialogResult To 'String' C#
C#
我正在使用 visual studio,使用 windows 形式,并得到一个错误:
Cannot Convert From System.Windows.Forms.DialogResult To 'String'
我正在尝试访问我计算机中已有的 Minecraft 文件,并希望将文件移动到特定文件夹,我让用户选择他们想要将 mod 移动到哪个世界,但是当我使用 System.IO.File.Move(datapack,DialogResult);
和 DialogResult
是我在此处创建的变量:DialogResult DialogResult = folderBrowserDialog1.ShowDialog();
并且计算机出于某种原因尝试将 DialogResult
转换为字符串但失败了。所以我查找了解决方案并找到了如何使用此函数 DialogResult.ToString();
将 DialogResult
转换为字符串,但出现了错误。
这是我的代码:(我的错误在第 29 行 System.IO.File.Move(datapack,DialogResult);
)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string username = Interaction.InputBox("You can find your username in C:/Users/Your name", "", "Put User Name In Here");
string lastD = @"\AppData\Roaming\.minecraft\saves";
folderBrowserDialog1.SelectedPath = @"c:\users\" + username + lastD;
DialogResult DialogResult = folderBrowserDialog1.ShowDialog();
folderBrowserDialog1.ShowNewFolderButton = false;
string datapack = @"C:\Program Files (x86)\Mr Snout's Datapack Installer\Datapacks\Nether Reactor.zip";
DialogResult.ToString();
System.IO.File.Move(datapack,DialogResult);
}
private void button2_Click(object sender, EventArgs e)
{
panel1.BringToFront();
panel1.Show();
}
private void button3_Click(object sender, EventArgs e)
{
panel1.SendToBack();
panel1.Hide();
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
}
}
}
您对什么是 DialogResult 及其用途有些困惑。它是一个枚举 DialogResult,用于 return 使用像 FolderBrowserDialog 这样的对话框时按下的按钮。它不是用户选择的文件夹的名称。这是由 SelectedPath 属性 编辑的 return。
因此您的代码在从对话框中获得结果后,应该检查用户是否按下了确定,然后构建要将源文件移动到的目标文件的名称。这可以通过从源中提取文件名并将其与对话框中选择的文件夹组合来完成。
private void button1_Click(object sender, EventArgs e)
{
string username = Interaction.InputBox("You can find your username in C:/Users/Your name", "", "Put User Name In Here");
string lastD = @"\AppData\Roaming\.minecraft\saves";
folderBrowserDialog1.SelectedPath = @"c:\users\" + username + lastD;
folderBrowserDialog1.ShowNewFolderButton = false;
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result == DialogResult.OK)
{
string datapack = @"C:\Program Files (x86)\Mr Snout's Datapack Installer\Datapacks\Nether Reactor.zip";
string destFile = Path.Combine(folderBrowserDialog1.SelectedPath, Path.GetFilename(datapack));
System.IO.File.Move(datapack,destFile);
}
}
DialogResult 不是 return 路径,而是结果 fx。 (好的,失败了)
相反,您使用您制作的 folderBrowserDialog1
,即 return 所示的路径:
System.IO.File.Move(datapack, folderBrowserDialog1.SelectedPath + "datapack.zip");
C# 我正在使用 visual studio,使用 windows 形式,并得到一个错误:
Cannot Convert From System.Windows.Forms.DialogResult To 'String'
我正在尝试访问我计算机中已有的 Minecraft 文件,并希望将文件移动到特定文件夹,我让用户选择他们想要将 mod 移动到哪个世界,但是当我使用 System.IO.File.Move(datapack,DialogResult);
和 DialogResult
是我在此处创建的变量:DialogResult DialogResult = folderBrowserDialog1.ShowDialog();
并且计算机出于某种原因尝试将 DialogResult
转换为字符串但失败了。所以我查找了解决方案并找到了如何使用此函数 DialogResult.ToString();
将 DialogResult
转换为字符串,但出现了错误。
这是我的代码:(我的错误在第 29 行 System.IO.File.Move(datapack,DialogResult);
)
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Microsoft.VisualBasic;
using System.Windows.Forms;
namespace WindowsFormsApp2
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
string username = Interaction.InputBox("You can find your username in C:/Users/Your name", "", "Put User Name In Here");
string lastD = @"\AppData\Roaming\.minecraft\saves";
folderBrowserDialog1.SelectedPath = @"c:\users\" + username + lastD;
DialogResult DialogResult = folderBrowserDialog1.ShowDialog();
folderBrowserDialog1.ShowNewFolderButton = false;
string datapack = @"C:\Program Files (x86)\Mr Snout's Datapack Installer\Datapacks\Nether Reactor.zip";
DialogResult.ToString();
System.IO.File.Move(datapack,DialogResult);
}
private void button2_Click(object sender, EventArgs e)
{
panel1.BringToFront();
panel1.Show();
}
private void button3_Click(object sender, EventArgs e)
{
panel1.SendToBack();
panel1.Hide();
}
private void vScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
}
}
}
您对什么是 DialogResult 及其用途有些困惑。它是一个枚举 DialogResult,用于 return 使用像 FolderBrowserDialog 这样的对话框时按下的按钮。它不是用户选择的文件夹的名称。这是由 SelectedPath 属性 编辑的 return。
因此您的代码在从对话框中获得结果后,应该检查用户是否按下了确定,然后构建要将源文件移动到的目标文件的名称。这可以通过从源中提取文件名并将其与对话框中选择的文件夹组合来完成。
private void button1_Click(object sender, EventArgs e)
{
string username = Interaction.InputBox("You can find your username in C:/Users/Your name", "", "Put User Name In Here");
string lastD = @"\AppData\Roaming\.minecraft\saves";
folderBrowserDialog1.SelectedPath = @"c:\users\" + username + lastD;
folderBrowserDialog1.ShowNewFolderButton = false;
DialogResult result = folderBrowserDialog1.ShowDialog();
if(result == DialogResult.OK)
{
string datapack = @"C:\Program Files (x86)\Mr Snout's Datapack Installer\Datapacks\Nether Reactor.zip";
string destFile = Path.Combine(folderBrowserDialog1.SelectedPath, Path.GetFilename(datapack));
System.IO.File.Move(datapack,destFile);
}
}
DialogResult 不是 return 路径,而是结果 fx。 (好的,失败了)
相反,您使用您制作的 folderBrowserDialog1
,即 return 所示的路径:
System.IO.File.Move(datapack, folderBrowserDialog1.SelectedPath + "datapack.zip");