Delete a file

Discussion in 'ASP .Net' started by Neven Klofuar, Sep 12, 2006.

  1. hi,

    I have a problem when trying to delete a file.

    I have to extract some information from a file, and then I have to delete
    it. When I try to delete it after I read it, I get a "Access denied" error.

    help pls, Neven

    ********************************
    StreamReader sr = new StreamReader(sFolderArhiva + sShortFileName);
    while ((sLine = sr.ReadLine()) != null) {
    try {
    if (sLine.Substring(0, "ApplicationItem=".Length) ==
    "ApplicationItem=") {
    docType =
    DohvatiTipDokumenta_TipDokumenta(sLine.Substring("ApplicationItem=".Length));
    }
    } catch (){}

    try {
    if (sLine.Substring(0, "ApplicationTag=".Length) ==
    "ApplicationTag=") {
    ID = sLine.Substring("ApplicationTag=".Length);
    }
    } catch {}
    }

    sr.Close();
    sr.Dispose();

    ------------

    try {
    File.Delete(sFolderArhiva + sShortFileName);
    } catch {}
    Neven Klofuar, Sep 12, 2006
    #1
    1. Advertising

  2. Hi Neven Klofuar,

    Are you sure you aren't holding the file in some other part of your code,
    or another program like a text editor. Can you delete the file manually?

    A tip when using disposable/closable objects use a using statement

    Instead of

    StreamReader sr = new StreamReader(sFolderArhiva + sShortFileName);
    // additional code here
    sr.Close();
    sr.Dispose();

    Use

    using(StreamReader sr = new StreamReader(sFolderArhiva + sShortFileName))
    {
    // additional code here
    }

    Makes the scope of the streamreader easier to see.

    --
    Happy Coding!
    Morten Wennevik [C# MVP]
    Morten Wennevik, Sep 12, 2006
    #2
    1. Advertising

  3. Neven,

    Check if ASPNET account (unless you use impersonation) has write access?

    Regards,
    Augustin


    "Neven Klofuar" wrote:

    > hi,
    >
    > I have a problem when trying to delete a file.
    >
    > I have to extract some information from a file, and then I have to delete
    > it. When I try to delete it after I read it, I get a "Access denied" error.
    >
    > help pls, Neven
    >
    > ********************************
    > StreamReader sr = new StreamReader(sFolderArhiva + sShortFileName);
    > while ((sLine = sr.ReadLine()) != null) {
    > try {
    > if (sLine.Substring(0, "ApplicationItem=".Length) ==
    > "ApplicationItem=") {
    > docType =
    > DohvatiTipDokumenta_TipDokumenta(sLine.Substring("ApplicationItem=".Length));
    > }
    > } catch (){}
    >
    > try {
    > if (sLine.Substring(0, "ApplicationTag=".Length) ==
    > "ApplicationTag=") {
    > ID = sLine.Substring("ApplicationTag=".Length);
    > }
    > } catch {}
    > }
    >
    > sr.Close();
    > sr.Dispose();
    >
    > ------------
    >
    > try {
    > File.Delete(sFolderArhiva + sShortFileName);
    > } catch {}
    >
    >
    >
    >
    >
    =?Utf-8?B?QXVndXN0aW4gUHJhc2FubmE=?=, Sep 12, 2006
    #3
  4. Thanks for the answers, I noticed that file I'm trying to delete has a
    read-only property, so I have to find a way to remove that property.

    Neven


    "Neven Klofuar" <neven.klofutar@**r.e.m.o..v...e**vip.hr> wrote in message
    news:e4RD%...
    > hi,
    >
    > I have a problem when trying to delete a file.
    >
    > I have to extract some information from a file, and then I have to delete
    > it. When I try to delete it after I read it, I get a "Access denied"
    > error.
    >
    > help pls, Neven
    >
    > ********************************
    > StreamReader sr = new StreamReader(sFolderArhiva + sShortFileName);
    > while ((sLine = sr.ReadLine()) != null) {
    > try {
    > if (sLine.Substring(0, "ApplicationItem=".Length) ==
    > "ApplicationItem=") {
    > docType =
    > DohvatiTipDokumenta_TipDokumenta(sLine.Substring("ApplicationItem=".Length));
    > }
    > } catch (){}
    >
    > try {
    > if (sLine.Substring(0, "ApplicationTag=".Length) ==
    > "ApplicationTag=") {
    > ID = sLine.Substring("ApplicationTag=".Length);
    > }
    > } catch {}
    > }
    >
    > sr.Close();
    > sr.Dispose();
    >
    > ------------
    >
    > try {
    > File.Delete(sFolderArhiva + sShortFileName);
    > } catch {}
    >
    >
    >
    >
    Neven Klofutar, Sep 12, 2006
    #4
  5. Removing the ReadOnly attribute can be done like this

    FileInfo fi = new FileInfo(sFolderArhiva + sShortFileName);

    if((fi.Attributes & FileAttributes.ReadOnly) > 0)
    fi.Attributes -= FileAttributes.ReadOnly;

    fi.Delete();

    --
    Happy Coding!
    Morten Wennevik [C# MVP]
    Morten Wennevik, Sep 12, 2006
    #5
  6. yup, found that one ...

    Thank you all for the help !!!

    Neven



    "Morten Wennevik" <> wrote in message
    news:eek:p.tfq8fqp9klbvpo@tr024...
    Removing the ReadOnly attribute can be done like this

    FileInfo fi = new FileInfo(sFolderArhiva + sShortFileName);

    if((fi.Attributes & FileAttributes.ReadOnly) > 0)
    fi.Attributes -= FileAttributes.ReadOnly;

    fi.Delete();

    --
    Happy Coding!
    Morten Wennevik [C# MVP]
    Neven Klofutar, Sep 12, 2006
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Sandeep Grover

    delete on delete !

    Sandeep Grover, Jul 12, 2003, in forum: C++
    Replies:
    19
    Views:
    604
    Chris \( Val \)
    Jul 22, 2003
  2. HeroOfSpielburg
    Replies:
    1
    Views:
    373
    Alf P. Steinbach
    Aug 6, 2003
  3. 0to60
    Replies:
    4
    Views:
    304
    Jerry Coffin
    Dec 19, 2003
  4. Mathieu Malaterre

    delete NULL, what about delete[] NULL

    Mathieu Malaterre, Aug 17, 2004, in forum: C++
    Replies:
    2
    Views:
    3,806
    Mathieu Malaterre
    Aug 17, 2004
  5. Replies:
    3
    Views:
    8,346
Loading...

Share This Page