Operator + in strings

string path = "C:\\Documents and Settings\\"+ LogonName +"\\Recent";


Error 1 Operator '+' cannot be applied to operands of type 'string' and 'method group'




What do i have to do


Thanks,
Guilherme


Answer this question

Operator + in strings

  • SUN999

    #region LogonName
    private void LogonName()
    {
    RegistryKey rkey = Registry.CurrentUser;
    RegistryKey rkey1 = rkey.OpenSubKey(
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer");

    string LogonName = rkey1.GetValue("Logon User Name").ToString();
    }
    #endregion LogonName

  • quicksilverm26

    You are receiving the error because your LogonName is a void method, meaning it will not return a value. The context in which you call the method expects it to return a string. Since it is void, it is going to throw an error.

  • Abe2244

    It looks like LogonName is a method, if so and it returns a string, try this:



    string path = "C:\\Documents and Settings\\"+ LogonName() +"\\Recent";



  • Agsathi

    Try changing it to this:

    private string LogonName()
    {
    RegistryKey rkey = Registry.CurrentUser;
    RegistryKey rkey1 = rkey.OpenSubKey(
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer");

    return rkey1.GetValue("Logon User Name").ToString();
    }



  • corradMSDN

    The code you've pasted above hasn't got any of the suggestions made above myself and Keith, that's why it's not working.

    Also you don't want to hard code the Documents and Settings folder as this can exist on different drives and locations (for example in Windows Vista the directory has been moved).

    Instead use:



    string recentFolderPath = Environment.GetFolderPath(Environment.SpecialFolder.Recent));



  • rcbailey56

    I Tried:

    string LogonName = Environment.UserName.ToString();
    label2.Text = LogonName.ToString();

    It Works, but when i tried:
    string pasta = "C:\\Documents and Settings\\" + LogonName +"Recent";

    i got the same error!

    Full code:

    using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Text;
    using System.Windows.Forms;
    using System.IO;
    using Microsoft.Win32;

    namespace Pc_Cleaner
    {
    public partial class Form1 : Form
    {
    public Form1()
    {
    InitializeComponent();
    LogonName();
    }


    #region LogonName
    private void LogonName()
    {
    RegistryKey rkey = Registry.CurrentUser;
    RegistryKey rkey1 = rkey.OpenSubKey(
    "SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Explorer");

    string LogonName = rkey1.GetValue("Logon User Name").ToString();
    }
    #endregion LogonName

    #region RecentFiles
    private void RecentFiles()
    {
    string pasta = "C:\\Documents and Settings\\" + LogonName +"Recent";
    DirectoryInfo di = new DirectoryInfo(pasta);
    FileInfo[] fi = di.GetFiles();
    foreach (FileInfo fiTemp in fi)
    fiTemp.Delete();
    }
    #endregion RecentFiles

    private void pictureBox1_Click(object sender, EventArgs e)
    {
    RecentFiles();
    }
    }
    }

  • Killjoyrules

    The code is pasted above was my old code, but i have tried both codes u and keith gaved me.

    It works with that Eviroment.GetFolderPath, that's what i need


    Thanks David em Keith, and sorry for any iconvinience



  • TedLee

    It looks as if your LogonName is declared as void. Can we see the code for this method

  • Redburga

    Same error :(

  • JasneetPaul

    Now i got:

    Error 1 Operator '+' cannot be applied to operands of type 'string' and 'void'

  • Goos van Beek

    Can you post the exact code you are using

    By-the-way, I wouldn't attempt to read the registry for the logon user name if that's what you trying to do, instead I would use the Environment.UserName property.



  • Operator + in strings