سؤال

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

هل كانت مفيدة؟

المحلول

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);
}

نصائح أخرى

Guid use factory pattern approach change

Guid filename = new Guid();

to

string filename = Guid.NewGuid().ToString();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top