Click here to Skip to main content
15,887,214 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
The issue I am having is I get an error "Cannot implicitly convert type System.Environment.SpecialFolder to String". Everything is working great except for btn4 where the error is. Line 61 is the issue "
if (windir = folderselect.RootFolder)
" Below is my code.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Forms;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using static System.Windows.Forms.VisualStyles.VisualStyleElement.Window;
using System.IO;
using static System.Windows.Forms.VisualStyles.VisualStyleElement;
using System.Windows.Shell;

namespace Client_Backup
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : System.Windows.Window
    {
        private string windir;
        public MainWindow()
        {
            InitializeComponent();
            windir = System.IO.Path.GetPathRoot(Environment.SystemDirectory);
            //tb2.Text = windir;
        }

        private void btn2_Click(object sender, RoutedEventArgs e)
        {
            for (int i = LB1.SelectedItems.Count - 1; i >= 0; i--)
            {
                LB1.Items.Remove(LB1.SelectedItems[i]);
            }
        }

        private void btn1_Click(object sender, RoutedEventArgs e)
        {
            if (new FolderBrowserDialog().ShowDialog() == System.Windows.Forms.DialogResult.OK)
                LB1.Items.Add(new FolderBrowserDialog().SelectedPath);
        }

        private void btn3_Click(object sender, RoutedEventArgs e)
        {

        }

        private void btn4_Click(object sender, RoutedEventArgs e)
        {
            //tb2.Text = windir;
            FolderBrowserDialog folderselect = new FolderBrowserDialog();
            if (folderselect.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                if (windir = folderselect.RootFolder)
                {
                    tb2.Text = "you have selected the system drive for a backup destination. please select a different drive.";
                }
                tb1.Text = (folderselect.SelectedPath);
            }
        }


        private void tb1_TextChanged(object sender, TextChangedEventArgs e)
        {
            tb1.Background = Brushes.FloralWhite;
        }

        private void btn5_Click(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void btn6_Click(object sender, RoutedEventArgs e)
        {
            if (this.WindowState == WindowState.Maximized)

                this.WindowState = WindowState.Normal;
            else
                this.WindowState = WindowState.Maximized;
        }

        private void btn7_Click(object sender, RoutedEventArgs e)
        {
            this.WindowState = WindowState.Minimized;
        }

        private void rectangle1_MouseDown(object sender, MouseButtonEventArgs e)
        {
            DragMove();
        }

        private void Cbx2Changed(object sender, RoutedEventArgs e)
        {
            if (cbx2.IsChecked == true)
            {
                LB1.IsEnabled = false;
                btn1.IsEnabled = false;
                btn2.IsEnabled = false;
            }
            else
            {
                LB1.IsEnabled = true;
                btn1.IsEnabled = true;
                btn2.IsEnabled = true;
            }
        }

        private void Cbx1Changed(object sender, RoutedEventArgs e)
        {

        }

    }
}


What I have tried:

There is no typo here. I have tried == as well as =. Using == introduces an additional error of "Operator == cannot be applied to operands of type string and Environment.SpecialFolder."
It appears to be a comparison issue that I can't get around. Everything else works fine...
Posted
Updated 6-Jul-23 17:57pm
Comments
Member 15627495 5-Jul-23 10:43am    
try with
Path.getFullPath(env..specialfolder)
bjwaldo 5-Jul-23 11:07am    
Thanks but I get the same error.

There is a method built into the Environment class that takes a special folder enum and returns the full path to that folder. The method is called GetFolderPath. You can use it in your code like.
C#
var path = Environment.GetFolderPath(Environment.Documents);
//compare path to your variable 
if(path == mypath) {
//TO DO
} 
 
Share this answer
 
Ha... I figured it myself.. Just had to go about it a bit differently.

private void btn4_Click(object sender, RoutedEventArgs e)
{
    FolderBrowserDialog folderselect = new FolderBrowserDialog();
    if (folderselect.ShowDialog() == System.Windows.Forms.DialogResult.OK)
    {
        if (System.IO.Path.GetPathRoot(folderselect.SelectedPath) == windir)
        {
            tb2.Text = "You have selected the system drive for a backup destination. Please select a different drive.";
        }
        else
        {
            tb2.Text = "";
        }
        tb1.Text = (folderselect.SelectedPath);
    }
}
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900