using System.IO;
public class TextFromFile
{
private const string FILE_NAME = "MyFile.txt";
public static void Main(String[] args)
{
if (!File.Exists(FILE_NAME))
{
Console.WriteLine("{0} does not exist.", FILE_NAME);
return;
}
using (StreamReader sr = File.OpenText(FILE_NAME))
{
String input;
while ((input=sr.ReadLine())!=null)
{
Console.WriteLine(input);
}
Console.WriteLine ("The end of the stream has been reached.");
sr.Close();
}
}
}
System.IO.FileInfo fi = new System.IO.FileInfo(filePath); //ファイルのサイズを取得 long filesize = fi.Length;
static UInt64 GetDiskFreeSpace(String DriveLetter)
{
try
{
Debug.Assert(DriveLetter.Length == 1);
ManagementObject mo = new ManagementObject("Win32_LogicalDisk=\"" + DriveLetter + ":\"");
UInt64 rc = Convert.ToUInt64(mo.Properties["FreeSpace"].Value); // 全容量なら "Size"
return rc;
}
catch (Exception ex)
{
Debug.Fail(ex.Message);
return 0;
}
}ZipFile zf = new ZipFile(zipFile); //zippedFileNameはZipInputStream.getNextEntry()であらかじめ取得しておく ZipEntry ze2 = zf.getEntry(ZippedFileName); FileSize = ze2.getSize(); CompressedFileSize = ze2.getCompressedSize(); zf.close(); //closeするのを忘れないこと!
Path.ChangeExtension()
FileStream fs_src = new FileStream(src, FileMode.Open, FileAccess.Read);
FileStream fs_dst = new FileStream(dst, FileMode.OpenOrCreate, FileAccess.Write);
byte[] buf = new byte[4096];
int len;
while ((len = fs_src.Read(buf, 0, buf.Length)) > 0)
{
fs_dst.Write(buf, 0, len);
}
fs_dst.Close();
fs_src.Close();using System;
using System.IO;
class Program
{
static void Main()
{
string path = @"C:\fdotnet\sample.txt";
File.Encrypt(path);
}
}FileAttributes atr = File.GetAttributes(path);
if( (atr & FileAttributes.ReadOnly) == FileAttributes.ReadOnly )
{
//リードオンリーだったらやる処理を書く
}//引数はフルパスで来ることを想定
protected String MakeUniqueFileName( String path )
{
String dir = Path.GetDirectoryName(path);
String ext = Path.GetExtension(path);
String fnwe = Path.GetFileNameWithoutExtension(path);
//ぶつからない名前になるまでまわす
int num = 1;
String newName ;
do
{
newName = dir + "\\" + fnwe + "(" + num + ")" + ext;
num++;
}
while(File.Exists(newName ));
return newName;
}StreamReader sr = new StreamReader(path, Encoding.Default);
String s = sr.ReadLine();
while(s != null)
{
//処理をする
}
sr.Close();System.Environment.GetFolderPath(System.Environment.SpecialFolder.Desktop);
または
System.Environment.GetFolderPath(System.Environment.SpecialFolder.DesktopDirectory);
この二つの違いは、上が仮想フォルダで下が物理フォルダとのことなのだが、実際に試すと同じ結果になるようで、正直よくわからない
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
Path.GetTempPath()
string stCurrentDir = System.IO.Directory.GetCurrentDirectory();
System.Windows.Forms.Application.StartupPath を参照
string s = System.Reflection.Assembly.GetExecutingAssembly().GetModules()[0]).FullyQualifiedName; //もしくは string s = System.Reflection.Assembly.GetExecutingAssembly().Locationこれはファイル名まで返す点に注意。フォルダ名だけが欲しい場合はPath.GetDirectoryName()をかます。
string appPath = Application.ExecutablePath;これもファイル名まで含みます
string m_Path = Assembly.GetExecutingAssembly().CodeBase ; string m_Dir = System.IO.Path.GetDirectoryName(m_Path);
これ↓だとキャッシュディレクトリが返る
path = this.GetType().Module.FullyQualifiedName;
//ヘッダ行なしの場合
CsvReader csv =new CsvReader(new StreamReader(FileName,Encoding.Default), false);
while(csv.ReadNextRecord())
{
String fld1 = csv[0];
String fld2 = csv[1];
...(適当になにかやる)
}