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.
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).
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(); }
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
SUN999
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
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
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
It works with that Eviroment.GetFolderPath, that's what i need
Thanks David em Keith, and sorry for any iconvinience
TedLee
Redburga
JasneetPaul
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.