从Appdata复制.sav文件并将其放置在桌面上的文件夹中

时间:2019-06-11 06:04:23

标签: c#

为了能够从appdata复制.sav文件并将其放置在桌面上的文件夹中。这是我玩的游戏,它使我自己和我的朋友更容易在桌面上保存文件,而不必去寻找它。

如何在appdata部分中指定文件的位置,一旦设置,请单击“保存”,它将文件放置在桌面上的文件夹中。

我还没有尝试过任何东西,因为不确定如何继续。

@Sach建议使用以下代码,但我不知道如何实现该代码。



Find AppData and Desktop folders:

var appData = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
var desktop = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);

Then there are a multitude of ways to move a file, this is one of them:

if (Directory.Exists(appData) && Directory.Exists(desktop))
{
    var file = Path.Combine(appData, "1.txt");
    if (File.Exists(file))
    {
        File.Move(file, Path.Combine(desktop, "1.txt"));
    }
}


要备份桌面上的保存文件。

1 个答案:

答案 0 :(得分:1)

希望这可以解决您的问题, 如果您还有其他问题,请随时问我。

这是WPF项目 XAML:

<Grid>
        <TextBox Name="txtSourceFile" HorizontalAlignment="Left" Height="21" Margin="11,23,0,0" TextWrapping="Wrap" IsEnabled="false" Text="" VerticalAlignment="Top" Width="442"/>
        <Button Margin="454,23,39,269" Content="..." Click="Button_Click" />
        <Label Content="SourceFile:" HorizontalAlignment="Left" Margin="10,-3,0,0" VerticalAlignment="Top"/>
        <TextBox Name="txtCopyPath" HorizontalAlignment="Left" Height="21" Margin="11,71,0,0" TextWrapping="Wrap" IsEnabled="false" Text="" VerticalAlignment="Top" Width="442"/>
        <Button Margin="453,71,40,221" Content="..." Click="Button_Click1" />
        <Label Content="Copy Path:" HorizontalAlignment="Left" Margin="10,45,0,0" VerticalAlignment="Top"/>
        <Button Margin="124,170,124,83" Content="Copy!" Click="Button_Click2" />
    </Grid>

XAML.CS:

using System;
using System.Windows;
using System.Windows.Forms;

namespace copy_file
{
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        private void Button_Click(object sender, RoutedEventArgs e)
        {
            try
            {
                System.Windows.Forms.OpenFileDialog dlg = new System.Windows.Forms.OpenFileDialog();
                dlg.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData);
                dlg.Filter = ".sav Files (*.sav)|*.sav|All files (*.*)|*.*";
                DialogResult result = dlg.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    txtSourceFile.Text = dlg.FileName;
                }
            }
            catch (Exception ex) { }
        }

        private void Button_Click1(object sender, RoutedEventArgs e)
        {
            try
            {
                FolderBrowserDialog dlg = new FolderBrowserDialog();
                dlg.SelectedPath = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);             
                DialogResult result = dlg.ShowDialog();
                if (result == System.Windows.Forms.DialogResult.OK)
                {
                    txtCopyPath.Text = dlg.SelectedPath;
                }
            }
            catch (Exception ex) { }           
        }

        private void Button_Click2(object sender, RoutedEventArgs e)
        {
            try
        {
            if(txtCopyPath.Text.Length>1 && txtSourceFile.Text.Length > 1)
            {                    
                string fName = System.IO.Path.GetFileName(txtSourceFile.Text);
                System.IO.File.Copy(txtSourceFile.Text, txtCopyPath.Text +"\\"+ fName, true);
                success = true;
            }
            System.Windows.MessageBox.Show("Info", success.ToString());
            success = false;
        }
            catch (Exception ex) { }
        }
    }
}