system.io

Getting Original Path from FileStream

Given a System.IO.FileStream object, how can I get the original path to the file it's providing access to? For example, in the MyStreamHandler() function below, I want to get back the path of the file that created the FileStream: public static void Main() { string path = @"c:\temp\MyTest.txt"; FileStream fs = File.Create(path)...

How to sort an array of FileInfo[] C#

I have the following code DirectoryInfo taskDirectory = new DirectoryInfo(this.taskDirectoryPath); FileInfo[] taskFiles = taskDirectory.GetFiles("*" + blah + "*.xml"); I would like to sort the list by filename. How is this done, as quickly and easily as possible using .net v2. Thank you ...

C# file exists by file name pattern

Using C# .net v2. I am using File.Exists(filepath). What I would like to do is swop this out for a pattern, because the first part of the filename changes... For example.. the file could be 01_peach.xml 02_peach.xml 03_peach.xml How can I check if the file exists based on some kind of search pattern? ...

Program to corrupt a file?

I know it might seem ridiculous that you would purposely want to corrupt a file, but I assure you its for a good reason. In my app, I have a lot of xml serialization going on. This in turn also means, I have a lot of deserialization. Today I tried some disaster scenarios. I reset the server during a serialization operation, as expect...

how to delete all files and folders in a directory?

Using C#, how can I delete all files and folders from a directory, but still keep the root directory? I have this System.IO.DirectoryInfo downloadedMessageInfo = new DirectoryInfo(GetMessageDownloadFolderPath()); foreach (FileInfo file in downloadedMessageInfo.GetFiles()) { file.Delete(); } foreach (DirectoryInfo dir in download...

How to test if directory is hidden in C#?

I have this loop: foreach (DirectoryInfo dir in downloadedMessageInfo.GetDirectories()) { if (dir.Attributes != FileAttributes.Hidden) { dir.Delete(true); } } How can I correctly skip all hidden directories? ...

.NET - Reliably Maintaining File System from an Application

I have a mixed application that has data both in a database and in a physical file store maintained by my application. As I have been developing my application I have, on occasion, run into scenarios where I am moving or deleting a file from the hard drive through my application and for whatever reason something will go wrong and an exce...

How to check if a file is in use?

Is there any way to first test if a file is in use before attempting to open it for reading? For example, this block of code will throw an exception if the file is still being written to or is considered in use: try { FileStream stream = new FileStream(fullPath, FileMode.Open, FileAccess.Read, FileShare.Read); } catch (IOException ...

output text file contents inline in an ASP.NET page

I have some HTML text files that I would like to dynamically include in an ASP.NET page. What is the best way to do this? My first guess was to create a Literal control on the page and output as follows: litMyLiteral.Text = System.IO.File.ReadAllText("c:\path\to\file.htm"); Is this a good solution, or is there a better one? For me, ...

System.IO Versus VisualBasic.FileIO

I typically develop in C# and am quite accustom to using System.IO. I recently ran across the FileIO library and have found that some of the advantages (such as sending an item to the recycle bin) are quite nice to have. Especially in the program I am currently working on. My question is, Does System.IO out perform the VisualBasic.FileIO...

File not found Exception.. But it's there

Hey this is going to be one of those dumb questions. I am trying to pick up a file on my local system and I keep getting a FileNotFoundException thrown. Someone set me straight please :) if( File.Exists(@"C:\logs\hw-healthways-prod_2009-08-26.tar")) { Console.WriteLine("Yay"); } else { ...

C# Winforms - Delete Folders on hard drive with specific file names?!?!

Hello! I want to be able to pass in the path of a folder to an application and have the program run through the entire contents of that folder, including nested folders and files, deleting any folder that it comes across which has a specific name. I have looked around for potential ways of doing this however I cannot seem to find any go...

Using the open file and save file dialogs in vb.net

Once a user has selected a file with the open file dialog, how can I handle this action? For example, if the user has selected a .txt file and has opened it, how can it get the data from the file? How can it return the path that the user found the file in? Then, how can it save the file? I know that there is a OpenFileDialog.OpenFile() ...

DirectoryInfo, FileInfo and very long path

I try to work with DirectoryInfo, FileInfo with very long path. I try use \\?\c:\long path (i got illegal caracter with fileInfo and DirectoryInfo) I try use file://c:/long path (i got uri not supported) Can i use ~ in a path or something else. I read this post but i would like to use another way that call a API. Is it any other sol...

Better way to check if Path is a File or a Directory ? (C#, .NET)

I am processing a TreeView of directories and files, users can select either a file, or a directory and then do something with it. This requires me to have a method which performs different actions if they have selected a file, or a directory. At the moment I am doing something like this to determine if the path is a file or a directory...

How do I check whether File.Delete() will succeed without trying it, in C#?

Hi, In C#, System.IO.File.Delete(filePath) will either delete the specified file, or raise an exception. If the current user doesn't have permission to delete the file, it'll raise an UnauthorizedAccessException. Is there some way that I can tell ahead of time whether the delete is likely to throw an UnauthorizedAccessException or not ...

ASP.NET Logon Failure IOException with Response.WriteFile but not with File.Copy?

I'm attempting to send a file to the browser that is located on a remote host (via UNC). The web app is running both under impersonation and an App Pool with a user that has full access to the remote host's UNC directory (via matching credentials). I can read and write files with no issues on the UNC path using the System.IO object's...

Directory.Exists - UNC Path with Access Denied C#

I am writing some code to access directories and index the files it finds. The user is able to enter a UNC path and impersonate another user to get access to that directory. I am using Directory.Exists to see if the path they have entered is valid before trying to access it. This works fine for local paths, but when trying to access a U...

What's the .Net replacement for File.Type FileSystemObject property?

In classic Asp we used the File.Type property to get the friendly name associated with a file type from the registry (e.g. "Text Document" for ".txt"). The FileInfo class which generally replaces the old COM object doesn't replicate this feature and so far I'm not having much success in my search of a replacement. ...

Rename image files on server directory

I need some help in renaming some images in a directory located at /images/graphicsLib/. All image names in /graphicsLib/ have naming convention that looks like this: 400-60947.jpg. We call the "400" part of the file the prefix and we call the "60957" part the suffix. The entire file name we call the sku. So if you saw the contents o...