Question

I use these following codes for using guid for file names in asp.net uploading forms.

protected void Button1_Click(object sender, EventArgs e)
{
    if (FileUpload1.HasFile == true)
    {
        Guid filename = new Guid();
        FileUpload1.SaveAs(Server.MapPath("~/upload/") + filename.ToString());
    }
}

But it only shows one file name and it's 00000000-0000-0000-0000-000000000000.

how can I solve this problem.

Thank in advance

Was it helpful?

Solution

change you code to this:

if (FileUpload1.HasFile == true)
{
    string filename = Guid.NewGuid().ToString(); // you used one extra new before Guid.NewGuid().ToString();
    FileUpload1.SaveAs(Server.MapPath("~/upload/") + filename);
}

OTHER TIPS

Guid use factory pattern approach change

Guid filename = new Guid();

to

string filename = Guid.NewGuid().ToString();
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top