1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How to work with alternate file streams on .NET?

Discussion in 'C#' started by andll, Jul 11, 2007.

  1. andll

    andll New Member

    Joined:
    Jul 11, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Good time of day for all!

    I have some troubles, and i hope you guys can help me. I search google, but did not find valid answer for my question.

    I want to open alternate file streams. If i write something like
    File.Open("filename:stramname")
    I receive expetion. It says, that file path in this format is not supported in .NET.

    Good, i found very simple solution, using CreateFile API and contructor in IO.FileStream class, that receive windows file handle.
    But.

    I have to use this code in ASP.NET pages, located in the remote server. And, it seems to me, hosting's security policies forbids applications to use Windows API.
    Anyway, i recieve <access denided> error, when use any Windows API on the ASP.NET pages, located on the hosting. I do not receive this errors on same code, if it executes on my local machine.

    So, i want to ask you: How can i open Stream object for the alternate file stream, without using Windows API?
     
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    filename:stramname should be a path/
     
  3. andll

    andll New Member

    Joined:
    Jul 11, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Wonderfull, but it does not work.
    I wrote:
     
  4. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Write it as File.Open(@"c:\temp.ext") where temp.ext is the actual file path.
     
  5. andll

    andll New Member

    Joined:
    Jul 11, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Oh, you really do not understand me.

    Ok, i ask question in another words.

    I have stream C:\XXX:ddd

    Can you write me there really working code, that opens this strram and reads all of data into string variable?
     
  6. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    Code:
    // create reader & open file
    Textreader tr = new StreamReader(@"C:\123.log");
    // read a line of text
    Console.WriteLine(tr.ReadLine());
    // close the stream
    tr.Close();
     
  7. andll

    andll New Member

    Joined:
    Jul 11, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    This code reads main stream of file C:\123.log
    I asked you, how to read file stream C:\XXX:ddd
    Can i give me this code, please?
     
  8. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,285
    Likes Received:
    364
    Trophy Points:
    83
    You cannot have a file in windows which has : in its name.
     
  9. andll

    andll New Member

    Joined:
    Jul 11, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    It is not file. This is alternate file stream(NTFS-stream).
    So, i ask how to work with it
     
  10. rhaazy

    rhaazy New Member

    Joined:
    Jul 6, 2007
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
  11. andll

    andll New Member

    Joined:
    Jul 11, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    This class library uses windows API, too
     
  12. rhaazy

    rhaazy New Member

    Joined:
    Jul 6, 2007
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    Uhhh, ok?

    If you still don't have an answer perhaps try asking a better question.

    Go to google and do a search for "alternate file streams .NET"
     
  13. andll

    andll New Member

    Joined:
    Jul 11, 2007
    Messages:
    7
    Likes Received:
    0
    Trophy Points:
    0
    Do i ask bad question?
    I ask how to work with AFS without importing WinAPI, is it hard to understantd?
     
  14. rhaazy

    rhaazy New Member

    Joined:
    Jul 6, 2007
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    I admit I did not notice at first glance the part of your inquiry where you stated "without API" , however if you would have just said what you just said which is: "how do I work with AFS without using API?" then that would have been the appropriate question.

    Code:
    using System; 
    using System.Runtime.InteropServices; 
    class TestNTFSStream 
    { 
     [DllImport("kernel32.dll")] 
     public static extern IntPtr CreateFile( string lpFileName, int dwDesiredAccess, int dwShareMode, 
      IntPtr lpSecurityAttributes, int dwCreationDisposition,  int dwFlagsAndAttributes, IntPtr hTemplateFile ); 
     // some constants look around for ideas of more to use
     private const int FILE_SHARE_READ = 1; 
     private const int OPEN_EXISTING = 3; 
     private const int INVALID_HANDLE_VALUE = -1; 
    
     static void Main(string[] args) 
     { 
    //Try to open the file for read only 
      IntPtr hflp = CreateFile( @"c:\filename:AFP_Resource", 0, FILE_SHARE_READ, IntPtr.Zero, OPEN_EXISTING, 0, IntPtr.Zero ); 
      if( (int)hflp == INVALID_HANDLE_VALUE ) 
       Console.WriteLine("No such file/stream"); 
      else 
       Console.WriteLine("File found"); 
     } 
    
    
    
    } 
    
     
  15. rhaazy

    rhaazy New Member

    Joined:
    Jul 6, 2007
    Messages:
    28
    Likes Received:
    0
    Trophy Points:
    0
    forgot to add this little bit of information that may also help.

    Check the MSDN doc's on deatail about PInvoke.
     

Share This Page